Veena Pointi
Ranch Hand
Posts: 442
posted 15 years ago
How to convert negative numbers to binary?
I thought negative numbers & positive numbers differ by most significant bits.But that is not true.For example if 10 is represented as
00000000 00000000 00000000 00001010
then 10 should be represented as
11111111 11111111 11111111 11111010
But that is wrong ,actual binary representation of 10 is
11111111 11111111 11111111 11110110
Can anybody explain how does this conversion happen?
Thanks
Veena
I thought negative numbers & positive numbers differ by most significant bits.But that is not true.For example if 10 is represented as
00000000 00000000 00000000 00001010
then 10 should be represented as
11111111 11111111 11111111 11111010
But that is wrong ,actual binary representation of 10 is
11111111 11111111 11111111 11110110
Can anybody explain how does this conversion happen?
Thanks
Veena
SCJP1.4
"Continuous effort  not strength or intelligence  is the key to unlocking our potential."
*Winston Churchill
Anthony Villanueva
Ranch Hand
Posts: 1055
posted 15 years ago
2's complement is used to indicate a negative number in binary representation.
In normal arithmetic, negative 2 is indicated by unary minus operator, so 2.
In binary it's not the case since there is no convenient way of "registering" the unary operator as a bit. So the notion of a complement came up.
Now a byte (the Java primitive) ranges from 128 to 127. If we list these values down we have something like
The values on the right column are of course the binary representations of the values in the left column. (I left a break between the binary because staring at a binary like 00101010 for long makes my eyes cross )
Anyway, representation in a byte is "halved". One half can represent 0 and positive integers up to 127 while the rest is given over to negative integers. Furthermore, each positive integer (and 0) is given a complement. This term makes sense if you consider that the binary representation of this complement is the "reverse" of the binary representation of the original number.
For example, the complement of 0 is 1. The binary representation of 0 is 0000 0000 while 1 is 1111 1111. Reversed, see?
So the complement of 1 is 2 since 1 is 0000 0001 while 2 is 1111 1110, etc.
To generalize, the complement of x is (x+1).
Finally, since this will come up, note that the largest positive value a byte can hold is 127 and its representation is 0111 1111. It follows that any binary representation starting with 1 is a negative number. This leftmost bit is called the significant bit, because it tells us if this number is + or .
So at a glance, 1101 0101 is a negative number. It's complement is 0010 1010 which is just 1 x 32 + 1 x 8 + 1 x 2 = 42 so 1101 0101 must be 43.
In normal arithmetic, negative 2 is indicated by unary minus operator, so 2.
In binary it's not the case since there is no convenient way of "registering" the unary operator as a bit. So the notion of a complement came up.
Now a byte (the Java primitive) ranges from 128 to 127. If we list these values down we have something like
The values on the right column are of course the binary representations of the values in the left column. (I left a break between the binary because staring at a binary like 00101010 for long makes my eyes cross )
Anyway, representation in a byte is "halved". One half can represent 0 and positive integers up to 127 while the rest is given over to negative integers. Furthermore, each positive integer (and 0) is given a complement. This term makes sense if you consider that the binary representation of this complement is the "reverse" of the binary representation of the original number.
For example, the complement of 0 is 1. The binary representation of 0 is 0000 0000 while 1 is 1111 1111. Reversed, see?
So the complement of 1 is 2 since 1 is 0000 0001 while 2 is 1111 1110, etc.
To generalize, the complement of x is (x+1).
Finally, since this will come up, note that the largest positive value a byte can hold is 127 and its representation is 0111 1111. It follows that any binary representation starting with 1 is a negative number. This leftmost bit is called the significant bit, because it tells us if this number is + or .
So at a glance, 1101 0101 is a negative number. It's complement is 0010 1010 which is just 1 x 32 + 1 x 8 + 1 x 2 = 42 so 1101 0101 must be 43.
May Pat
Ranch Hand
Posts: 32
posted 15 years ago
FYI. I found this site http://www.learnbinary.com/TwosComplement.html to be a very useful tool to learn 2's complement.
May P.
Michael Naz
Greenhorn
Posts: 2
posted 15 years ago
There's a pretty easy way to convert a positive number to its negative compliment (2's compliment). First you find 1's compliment, which is just reversing the sign of every bit. Then add 1 to get 2's compliment. Here's an example with the number 10 (using 16 bits):
Start with the positive number:
00000000 00001010
Get 1's compliment by reversing the bits:
11111111 11110101
Then add 1:
11111111 11110101
+ 1

11111111 11110110 = 10
That's it. To convert a negative number to its positive compliment just reverse the process: subtract 1 from the number, then get 1's compliment (reverse the sign of each bit).
There are other ways to get 2's compliment but I find that this is the easiest and quickest. Hope it helps!
Start with the positive number:
00000000 00001010
Get 1's compliment by reversing the bits:
11111111 11110101
Then add 1:
11111111 11110101
+ 1

11111111 11110110 = 10
That's it. To convert a negative number to its positive compliment just reverse the process: subtract 1 from the number, then get 1's compliment (reverse the sign of each bit).
There are other ways to get 2's compliment but I find that this is the easiest and quickest. Hope it helps!
Veena Pointi
Ranch Hand
Posts: 442
Veena Pointi
Ranch Hand
Posts: 442
posted 15 years ago
Converting negative numbers to binar is clear to me now.But I am not knowing how to convert binary representation of negative numbers to decimal.Can someone explain this to me?
Thanks
Veena
Thanks
Veena
SCJP1.4
"Continuous effort  not strength or intelligence  is the key to unlocking our potential."
*Winston Churchill
Anthony Villanueva
Ranch Hand
Posts: 1055
posted 15 years ago
Hi Veena.
This is how I do it:
1. get the complement
2. change this binary to decimal using the usual expansion
3. add one to the decimal value
That's your negative number in decimal form.
For example: given 11010101
1. complement is 00101010
2. expanding, I have 0x128 + 0x64 + 1x32 + 0x16 + 1x8 + 0x4 + 1x2 + 0x1 = 32 + 8 + 2 = 42
3. adding one, I have 43
so I have 43
This is how I do it:
1. get the complement
2. change this binary to decimal using the usual expansion
3. add one to the decimal value
That's your negative number in decimal form.
For example: given 11010101
1. complement is 00101010
2. expanding, I have 0x128 + 0x64 + 1x32 + 0x16 + 1x8 + 0x4 + 1x2 + 0x1 = 32 + 8 + 2 = 42
3. adding one, I have 43
so I have 43
Kumar Kausikasa
Ranch Hand
Posts: 45
posted 15 years ago
Hi,
I found out this can be easier.
Suppose the number is 3; it binary representation is 0000 0011
to represent 3 in binary do this
1. Start from right to left.
2.After you ecounter the first one, after that inverse all the bits, (means make 0's to 1 and i 's to 0)
here 0000 0011 ( 3 in binary)
start from right of least significant bit
0000 0011
^

this is the first one after that invert all the bits except the first one
1111 1101
 ^
  (the first one remains same)

 > one becomes 0
___ 0 becomes 1.
HTH
Praveen.
I found out this can be easier.
Suppose the number is 3; it binary representation is 0000 0011
to represent 3 in binary do this
1. Start from right to left.
2.After you ecounter the first one, after that inverse all the bits, (means make 0's to 1 and i 's to 0)
here 0000 0011 ( 3 in binary)
start from right of least significant bit
0000 0011
^

this is the first one after that invert all the bits except the first one
1111 1101
 ^
  (the first one remains same)

 > one becomes 0
___ 0 becomes 1.
HTH
Praveen.
Kumar Kausikasa
Ranch Hand
Posts: 45
Veena Pointi
Ranch Hand
Posts: 442