size of long

P

pembed2003

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!
 
V

Victor Bazarov

pembed2003 said:
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
 
S

Siemel Naran

pembed2003 said:
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)
 
J

Jacek Dziedzic

pembed2003 said:
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.
 
V

Victor Bazarov

Jacek Dziedzic said:
pembed2003 said:
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
 
B

Bill Seurer

pembed2003 said:
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?
 
O

Old Wolf

Bill Seurer said:
pembed2003 said:
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).
 
J

Jacek Dziedzic

Bill said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top