# Value of ~10

Swathi Sree

Greenhorn

Posts: 19

posted 10 years ago

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.

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

Posts: 7729

posted 10 years ago

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

Posts: 19

posted 10 years ago

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.

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

Posts: 7729

posted 10 years ago

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 ]

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 ]

Getting someone to think and try something out is much more useful than just telling them the answer.

Rick O'Shay

Ranch Hand

Posts: 531

posted 10 years ago

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

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

Sheriff

Posts: 14112

posted 10 years ago

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

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