Kbase P55038: Why getting unexpected results working with large integer nu
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  19/11/2003 |
|
Status: Unverified
GOAL:
Why getting unexpected results working with large integer numbers ?
GOAL:
Why 256 * 256 * 256 * 256 is 0 ( zero ) while 1.00 * 256 * 256 * 256 * 256 is 4,294,967,296 ?
FACT(s) (Environment):
INTEGER data type are signed values on 32 bits.
This means that the range is [-2^31, 2^31 - 1] or [-2,147,483,648 2,147,483,647]
Now 256 * 256 * 256 * 256 is treated as an integer value while 1.00 * 256 * 256 * 256 * 256 is a decimal value. Still, why zero ?
In the binary mode 256 * 256 * 256 * 256 is
1 0000 0000 0000 0000 0000 0000 0000 0000.
Because integers are signed value on 32 bits we handle the first 32 bits from the right and ignore the first bit from the left. So the result is
0000 0000 0000 0000 0000 0000 0000 0000 i.e. 0 ( zero ).
Now why 128 * 256 * 256 * 256 is -2,147,483,648 ?
In binary mode the result will be
1000 0000 0000 0000 0000 0000 0000 0000
Because the first bit from the left is 1 this means we are dealing with a negative number.
All we have to do now is drop the first bit from the left, reverse all the other bits , add 1 and the minus sign.
So 000 0000 0000 0000 0000 0000 0000 0000 reversed is
111 1111 1111 1111 1111 1111 1111 1111 which is 2,147,483,647.
Adding 1 and minus sign we get - ( 2,147,483,647 + 1 )
i.e. -2,147,483,648 i.e. - 2^31.