# size of long

Discussion in 'C++' started by pembed2003, Apr 23, 2004.

1. ### pembed2003Guest

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!

pembed2003, Apr 23, 2004

2. ### Victor BazarovGuest

"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. 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

Victor Bazarov, Apr 24, 2004

3. ### Siemel NaranGuest

"pembed2003" <> 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)

Siemel Naran, Apr 24, 2004
4. ### Jacek DziedzicGuest

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.

Jacek Dziedzic, Apr 24, 2004
5. ### Victor BazarovGuest

"Jacek Dziedzic" <> 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

Victor Bazarov, Apr 24, 2004
6. ### Bill SeurerGuest

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?

Bill Seurer, Apr 26, 2004
7. ### Old WolfGuest

Bill Seurer <> 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).

Old Wolf, Apr 26, 2004
8. ### Jacek DziedzicGuest

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.

Jacek Dziedzic, Apr 28, 2004