Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integers and out of range

 
Jeroen de Wolf
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book SCJP Study Guide from Kathy Sierra which is by the way an excelent book explains the principle of integer overflow by assigning a hexadecimal nr. 0XDEADCAFE to an int variable. The compiler accepts this and because its out of range, it rolls over to negative.
Subsequently I tried to assign the decimal representation of 0XDEADCAFE to the int variable (3735931646) but then the compiler complains about the variable being out of range.
In my humble opinion the value 0XDEADCAFE and 3735931646 are equal and differ only in representation. Why does the compiler treats them differently?
Kind regards,
Jeroen de Wolf
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jeroen -
An int is 32 bits, always signed, which means it can handle the range -2^31 to (2^31)-1. In decimal, the largest value is 2147483648. The number you selected, 0XDEADCAFE, is a negative number because the most significant bit is 1. Now, in C++, which supports and unsigned int, you could assign that value as a positive value 3735931646, but not in Java. Instead, you'd have to assign it to a long, which is 64 bits.
To assign OXDEADCAFE as a decimal number, you'd have to find its negative value through 2's complement conversion.
Hope that helps.
[ October 10, 2003: Message edited by: Jeff Bosch ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words:
0XDEADCAFE and 3735931646 are not equal. 0XDEADCAFE is a negative number.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The book SCJP Study Guide from Kathy Sierra which is by the way an excelent book explains the principle of integer overflow by assigning a hexadecimal nr. 0XDEADCAFE to an int variable. The compiler accepts this and because its out of range, it rolls over to negative.
Subsequently I tried to assign the decimal representation of 0XDEADCAFE to the int variable (3735931646) but then the compiler complains about the variable being out of range.
In my humble opinion the value 0XDEADCAFE and 3735931646 are equal and differ only in representation. Why does the compiler treats them differently?
Kind regards,

When you are assigning a hex number to an int, you are actually telling the compiler the bit values. That is:
0XDEADCAFE
is equal to 32 bits. They will be copied to int's 32 bits. And because MSB (32nd bit) is 1, the number is treated as negative: -559035650.
However, when you try to assign a decimal number (e.g. 3735931646) to an int, the compiler first has find it's 2's complement. It uses the 32nd bit (MSB) as sign bit which will be 0 in this case. The remaining 31 bits can not accomodate the give number.
Hope this makes sense.
Barkat
[ October 10, 2003: Message edited by: Barkat Mardhani ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic