• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Convert hexa to decimal.

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Why the following Java code prints Negative value instead of positive.

class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.print(x);
}
}


Output:- -2147483648
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the left-most bit is a sign bit. If the sign bit is a 1, the number is negative. If it's a 0, it's positive. The value you have has a 1 in the sign bit, so it is interpreted as a negative number.

Negative Numbers in Java - Two's Complement

Hex to Decimal and Back Again...
[ January 03, 2006: Message edited by: Corey McGlone ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
converting 0x80000000 to devimal gives

1000 0000 0000 0000 0000 0000 0000 0000
----------------------------------------
8 0 0 0 0 0 0 0

as the most significant bit is 1 this negative number so decimail equivalent is - 2(pow)31

regards,
santosh
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and for those of you keeping score, this is a 1.4 topic, but NOT a 1.5 topic!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
...and for those of you keeping score, this is a 1.4 topic, but NOT a 1.5 topic!



Oh, man! They always take away the easy questions.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic