• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Integers and out of range

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words:
0XDEADCAFE and 3735931646 are not equal. 0XDEADCAFE is a negative number.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic