Any way to get the limits of an integer using a code?
There are two ways:
1.Simpler one
#include
#include
int main()
{
printf("Range of int: %d to %d\n", INT_MIN, INT_MAX);
}
2. Another One
upper_limit=(~(0u)>>1);
0u
fill in all 0s in the register so it looks like 000...000 (represented as MSB...LSB)
~(0u)
Now, flip all bits so it looks like 111...111
~(0u)>>1
Shift right one bit so that MSB is zero
011...111 which is maximum positive number that you can represent.
for lower range:
lower_limit=( (~0)^(~0u>>1)) (or lower_limit = ~upper_limit) logic flips 011...111 to 100...000 which is the lowest number presentable.
Saturday, December 30, 2006
Integer Limits
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment