• 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

Question on DataTypes

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a integer whose value in decimal is -119
Below line of code displaying value as ffffff89
Integer.toHexString((new Integer("-119")).intValue()));

Can you help me in understanding what's happening behind the scenes, how java process the negative numbers?

Thank you in advance,
Bhanu
[ March 18, 2008: Message edited by: Bhanu S Chatta ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Computer Science 101: Two's Complement storage of signed integers.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An integer is stored in 32 bits, with the left-most bit being the sign bit - 1 for negative, 0 for positive. This is called binary. A negative number is computed by the two's complement - you switch all bits (from 0 to 1 and vice versa), then add 1.

Now -119 is in binary:
1111 1111 1111 1111 1111 1111 1000 1001

This number is calculated as follows:
119 is 1x 64 + 32 + 16 + 0 + 4 + 2 + 1, or in binary:
0000 0000 0000 0000 0000 0000 0111 0111

The two's complement leads to the above binary form of -119.

Now hexidecimal works by converting each of these blocks of 4 into a number between 0 and 15 (inclusive), with 10-15 being represented as A-F.
So the above becomes F F F F F F 8 9 - just as you had found out.
 
Bhanu S Chatta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You!! That was helpful
 
reply
    Bookmark Topic Watch Topic
  • New Topic