Velocity Reviews > C++ > size of long

# size of long

pembed2003
Guest
Posts: n/a

 04-23-2004
Hi all,
As an exercise, I am trying to figure out the size of a long in my
machine without using the sizeof operator. I came up with the
following:

int size_of_long(){
long i = 1,c = 1;
while(i > 0){
i<<=1;
c++;
}
return c / 8;
}

This works fine but I am afraid that if the code is being run in a
machine where it doesn't use 2's compliment. It will not work so I
came up with another function:

int size_of_long2(){
long i[2];
return (long)(i+1) - (long)i;
}

This works regardless of what machine the code is running. I wonder if
there is any other ways to determine the size of long?

Thanks!

Victor Bazarov
Guest
Posts: n/a

 04-24-2004
"pembed2003" <(E-Mail Removed)> wrote...
> Hi all,
> As an exercise, I am trying to figure out the size of a long in my
> machine without using the sizeof operator. I came up with the
> following:
>
> int size_of_long(){
> long i = 1,c = 1;
> while(i > 0){
> i<<=1;
> c++;
> }
> return c / 8;

Why over 8? Shouldn't it be char_bits() or some such, implemented
the same way?

> }
>
> This works fine but I am afraid that if the code is being run in a
> machine where it doesn't use 2's compliment. It will not work so I
> came up with another function:
>
> int size_of_long2(){
> long i[2];
> return (long)(i+1) - (long)i;
> }
>
> This works regardless of what machine the code is running. I wonder if
> there is any other ways to determine the size of long?

Determine the size of 'unsigned long' and report it. IIRC, unsigned
versions of the arithmetic types have the same size as their signed
counterparts. For the 'unsigned long' the operation is well-defined
and they simply turn 0 after you shift them one time too much.

Victor

Siemel Naran
Guest
Posts: n/a

 04-24-2004
"pembed2003" <(E-Mail Removed)> wrote in message

> As an exercise, I am trying to figure out the size of a long in my
> machine without using the sizeof operator. I came up with the
> following:

Without pre-processor: sizeof(long)

Jacek Dziedzic
Guest
Posts: n/a

 04-24-2004
pembed2003 wrote:
> Hi all,
> As an exercise, I am trying to figure out the size of a long in my
> machine without using the sizeof operator. [...]

Try

#include <iostream>
#include <limits>

int main() {
std::cout << std::numeric_limits<unsigned long int>::digits
<< std::endl;
}

This gives you the number of bits in "unsigned long int",
which is 32 on my system, for example. I'm not sure though
it's exactly synonymous with sizeof(unsigned long int).

HTH,
- J.

Victor Bazarov
Guest
Posts: n/a

 04-24-2004
"Jacek Dziedzic" <(E-Mail Removed)> wrote...
> pembed2003 wrote:
> > Hi all,
> > As an exercise, I am trying to figure out the size of a long in my
> > machine without using the sizeof operator. [...]

>
> Try
>
> #include <iostream>
> #include <limits>
>
> int main() {
> std::cout << std::numeric_limits<unsigned long int>::digits
> << std::endl;
> }
>
> This gives you the number of bits in "unsigned long int",
> which is 32 on my system, for example. I'm not sure though
> it's exactly synonymous with sizeof(unsigned long int).

Of course it's not. It's synonymous with sizeof(unsigned long)*CHAR_BIT

Victor

Bill Seurer
Guest
Posts: n/a

 04-26-2004
pembed2003 wrote:

> int size_of_long2(){
> long i[2];
> return (long)(i+1) - (long)i;
> }
>
> This works regardless of what machine the code is running.

Hmmm. Is it guaranteed that the compiler will not pad between elements
of an array of simple types?

Old Wolf
Guest
Posts: n/a

 04-26-2004
Bill Seurer <(E-Mail Removed)> wrote:

> pembed2003 wrote:
>
> > int size_of_long2(){
> > long i[2];
> > return (long)(i+1) - (long)i;
> > }
> >
> > This works regardless of what machine the code is running.

>
> Hmmm. Is it guaranteed that the compiler will not pad between elements
> of an array of simple types?

Yes, but it's not guaranteed that it will convert from (long *) to (long).
Try:
return (char *)(i+1) - (char *)i;

Also, long i[1]; would have been sufficient (you're allowed to point
one-past-the-end of an array as long as you don't dereference).

Jacek Dziedzic
Guest
Posts: n/a

 04-28-2004
Bill Seurer wrote:

> Hmmm. Is it guaranteed that the compiler will not pad between elements
> of an array of simple types?

Yes, it is! I'd asked that on this ng once, so now I know

- J.