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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Binary for Negative number

Greenhorn
Posts: 6
Hello All
I am not clear on how to convert a negative number to binary.
I used the toBinary method in wrapper class to make some sense but its not clear.
Binary for 1 = 0001
whats binary for -1 ?
Binary for 2 = 0010
whats binary for -2 ?

Should I just reverse it...so -1 will be 1110 and -2 will be 1101. I am not clear. Please advice
Thank you

Sheriff
Posts: 11343

Originally posted by Shal Kaushik:
...Should I just reverse it...so -1 will be 1110 and -2 will be 1101. I am not clear...

That's close. Actually, you flip the bits (exchange all the zeros and ones), then add one. So -1 is ...1111, and -2 is ...1110.

[ January 19, 2006: Message edited by: marc weber ]

Ranch Hand
Posts: 91
if you would just invert it, you would have two representations for zero (0000 and 1111)... so they came up with the solution not to represent "-0", and shift all negative values up by one, so after inversion, simply do a binary "add 1", so that the (otherwise wasted) 1111 represents -1, and 1110 is -2 instead of -1 etc.

this also explains why the range of values is unsymmetric (eg. a byte is from -2^8 to 2^8-1)

Ranch Hand
Posts: 961
I am going to exapain it with 4-bit-signed integer for you to understand:

These are posible values for a 4-bit-signed integer in the positive side with the 4th bit reserved for the sign

(sign)
----4-----3----2----1----decimal
----0-----0----0----0 = 0
----0-----0----0----1 = 1
----0-----0----1----0 = 2
----0-----0----1----1 = 3
----0-----1----0----0 = 4
----0-----1----0----1 = 5
----0-----1----1----0 = 6
----0-----1----1----1 = 7

Notice how the 4th bit is never used, because it represents the sign, in this case 0 means positive. Notice also that because we cannot use the last bit, this 4-bit integer cannot reach the number 8. So this number limits are, in the positive side, 2^3-1.

Let's see the negative side:

Now, remeber that to convert a binary number to negative, you simple invert the bits and add 1, that's to say ~x+1 is the negative value of x. In other words: x == | ~x+1 | is true.

For instance, let's convert 5 (0101) to a negative number. I will not take into account the sign. Let's say x=5

~101 //invert bits (~x)
-----
=010 //partial result
-----
=011 //partial result

The negative number is 1011 with the first bit turned on, because this is a negative number.

Hence, the negative numbers are:

(sign)
----4-----3----2----1----decimal
----1-----0----0----0 = -8
----1-----0----0----1 = -7
----1-----0----1----0 = -6
----1-----0----1----1 = -5
----1-----1----0----0 = -4
----1-----1----0----1 = -3
----1-----1----1----0 = -2
----1-----1----1----1 = -1

Notice how in this case, the negative numbers take adventage of their additional bit, and that is why negative numbers are always one number greater thant the positive ones. The limits of the negative numbers are, in this case: 2^3. In the positive side we could not express the number 8, because we were a bit short.

Play a bit with the Integer.toBinaryString() and you will see. Of course, in that case you would be playing with 32-bin-signed integers.

Regards,
Edwin Dalorzo
[ January 19, 2006: Message edited by: Edwin Dalorzo ]

Shal Kaushik
Greenhorn
Posts: 6
THANK YOU So MUCH It makes absolute sense

Greenhorn
Posts: 4
Hi Shal,

A shortcut method for finding the negative no of a binary is

"Starting from the right i.e LSB (Least Significant Bit) you find the first one bit. From LSB till the first 1 bit, including the 1 bit keep all the bits as it is and after the 1 bit interchange all other bit 1s to 0s and 0s to 1s"

eg;

x=001010

from right keep 10 as it is
remaining part 0010 - interchanging 0s to 1s and 1s to 0s we get 1101
combining the above 2 steps we get

-x=110110

Ranch Hand
Posts: 3061
For your information, this method is called "twos compliment". I just thought I would mention tis so you can google for more info if you are interested.

Layne

 Are you here to take over the surface world? Because this tiny ad will stop you! Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton