my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Value of ~10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Value of ~10" Watch "Value of ~10" New topic
Author

Value of ~10

Swathi Sree
Greenhorn

Joined: Aug 27, 2005
Posts: 19
Hi All,

I am not able to understand how come ~10 is displaying -11?
the Binary value of 10 is 1010 and one's complement gives 0101 which is 5. As the sign bits are changed, it should be -5. If I print the binary string, then also it is printing "11111111111111111111111111110101".
But if I print toString it is again giving same -11.

Any body please clarify how it is displaying -11.

Thanks in Advance,

Regards,
Swathi.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hint: there are 32 bits in an int. You are only considering 4 of them. What about the other 28 bits?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Swathi Sree
Greenhorn

Joined: Aug 27, 2005
Posts: 19
Thanks for pointing out that only 4 bits are considered. Even if I consider all bits except the top most bit(sign bit) ita giving 2147483637. resulting in negative sign gives -2147483637.

To get the decimal number of the ones complementation of 10, I used calculator.But in the exam that won't be available(I am preparing for the java certification). Is there any way to calculate this easily?

Thanks & Regards,
Swathi.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
First a bit of maths: By using 2 complement we get -x = ~x + 1.
So ~x = -x - 1 = -(x + 1). Using that, ~10 = -(10 + 1) = -11.

Now let's try with bits:

[ August 27, 2005: Message edited by: Barry Gaunt ]
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
What you are missing is that Java uses twos complement.

You took the complement of 10 correctly. I will round off to a byte for clarity:

00001010 -> 11110101

To get the value of a negative number you need to take the twos complement: flip and add one and negate. Try it, it's -11!

11110101 -> 00001010 -> 00001011 -> 11 -> -11
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words, the sign bit doesn't work the way you think it works. Putting a sign bit in front of a binary 5 doesn't make it a -5.

There are at least two reasons to use the two's complement:

- you don't waste the zero (if it worked like you thought, there also would be a negative zero), and

- addition with negative numbers works *exactly* like with positive numbers. That is, if you have four bit numbers



The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value of ~10