• 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

p169 K&B int assignment question.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is it legal to assign a hex value that is out of int range to an int variable?
int x1 = 0x80000000;
why is this legal? this is like assigning 2147483648 to an int ?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ox800000000 is most negative int value -2e31 which is -2147483648.
 
Ian Wu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam sorry, I still don't understand.
0x80000000 means 80000000 in hexidecimal.
if my math is correct converting 80000000 from hex to dec, the answer should be 2147483648
so the code:
int x = 0x80000000;
Is equivalent in assigning 2147483648 to and int then?
Isn't that value too big for an int variable to store?
Why would the compiler say it is legal to do so?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Ian.
integers are signed in Java. They are represented in two's complement. Any int with the most significant bit to 1 is a negative number. This is the reason why 0x800000000 yields -2147483648. It is an int that is in range : from -2147483648 to 2147483647
The compiler however will complain if the number is out of range:
int x = 0x80000000000000000000;
 
reply
    Bookmark Topic Watch Topic
  • New Topic