• 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

Value of hexadecimal 0xDeadCafe

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While reading the book Sun Certified Programmer & Developer for Java 2 I found the following example:

class HexTest {
public static void main (String [] args ) {
int x = 0X0001;
int y = 0x7fffffff;
int z = 0xDeadCafe;
System.out.println("x= " + x +" y= " + y + " z= " + z);
}
}


This produces the following output:
x=1 y=2147483647 z=-559035650

I understand x and y but not z. When I use the calculator in Windows and type DeadCafe in hexadecimal mode the decimal result I get is 3 735 931 646. As far as I know an int is capable of representing 2^32 that is 4 294 967 296. So my question is, how do I get the same result as the book?

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an int is 32 bits (if i remember right). that give you 2^32 values, but they are not all positive.

the first bit (leftmost) is used to represent the sign of the number. so, 1/2 of the 2^32 are negative, and 1/2 are non-negative (which includes 0).

search around in this forum for "two's complement", and you see how negative numbers are represented in java.

You calcuator is not limited to 32 bits, so it can display larger numbers.
 
Eva Schopi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick response!
(I should have thought about that!!! )
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic