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