Wednesday, 5 September 2012

Representation of Negative number



Representing Negative Numbers: Two’s Complement


one must often deal with negative numbers; how can they be represented as well? One obvious
solution would be to use a single bit to represent the sign of the number (+ or −) and the remaining

bits to represent the magnitude of the number (how positive or negative it is). In such a signed
magnitude representation, a most-significant bit of 0 represents “+” while a most-significant bit
of 1 represents “−”; the remaining bits give the magnitude of the number. For example, an 8-bit
signed magnitude representation of 13 is 00001101 while −13 is 10001101. Note that using 8-bit
signed magnitude, one can represent integers in the range −127 (11111111) to 127 (01111111).
Signed magnitude has one peculiarity, however. The integer 0 can be represented in two ways:
00000000 = +0 and 10000000 = −0.

By far, the most common representation of positive and negative integers is two’s complement.
In two’s complement, positive integers are represented in standard binary, as in signed magnitude.
However, the representation of a negative number is determined as follows: (1) compute a binary
representation of the magnitude of the number, (2) flip all the bits, and (3) add 1. For example,
the 8-bit two’s complement representation of 13 is 00001101 (as before) while −13 is represented
as follows (using the steps given above):

−13
(1)
=) 00001101
(2)
=) 11110010
(3)
=) 11110011

Note that a most-significant bit of 1 again signifies a negative number, but the remaining bits do
not encode the magnitude in the usual way. Here are a few more examples of converting integers
to 8-bit two’s complement form (remember that non-negative integers are represented in standard
binary):

15 =) 00001111
−15
(1)
=) 00001111
(2)
=) 11110000
(3)
=) 11110001
28 =) 00011100
−28
(1)
=) 00011100
(2)
=) 11100011
(3)
=) 11100100

To convert a negative two’s complement number back to decimal, follow these steps: (1) flip all the
bits, (2) add 1, and (3) interpret the result as a binary representation of the magnitude and add a
negative sign. For example,
11110011
(1)
=) 00001100
(2)
=) 00001101
(3)
=) −13

Here are a few more examples of converting 8-bit two’s complement back to decimal (remember
that if the number begins with a 0, it’s a non-negative integer represented in standard binary):


Representation of negative numbers
 
Signed-Magnitude representation:
·  This is the simplest method.
·  Write the magnitude of the number in binary. Then add a 1 to the front of it if the
number is negative and a 0 if it is positive.
·  Examples: +7 would be 111 and then a 0 in front so 00000111 for an 8-bit
representation.
-9 would be 1001 (+9) and then a 1 so 10001001 for an 8-bit representation.
·  It is not the best method or representation because it makes computation
awkward.
 
2’s complement representation:
 
·  Most widely used method of representation.
·  Positive numbers are represented as they are (simple binary).
·  To get a negative number, write the positive number in binary, then change all 0’s
to 1’s and 1’s to 0’s. Then add 1 to the number.
·  Example : +7 would be 0111 in 4-bit 2’s complement.
To represent –5 we take +5 (0101) and then invert the digits (1010) and add 1
(1011). –5 is thus 1011.
·  Suppose you already have a number that is in two’s complement representation
and want to find its value in binary.
If the number starts with a 1 it is a negative number. If it starts with a 0 it is a
positive number.
If it is a negative number, take the 2’s complement of that number. You will get
the number in ordinary binary. The sign you already know. Let’s take 1101.
Take the 2’s complement and you get 0011. Since it started with a 1, it was
negative and the value is 0011 which is 3. The number represented by 1101 is –3
in 2’s complement.
Lets see how this system is better:
If we add +5 and -5 in decimal we get 0.
Let’s add them in 4-bit signed-magnitude. +5 is 0101 and –5 is 1101. On adding we get
10010. That is not zero.

Let’s do the same thing in 2’s complement. Adding 0101 (+5) and 1011 (-5) gives
10000. If we discard the carry of 1 we get 0000 – i.e. 0.
Thus addition works out ok for negative numbers in 2’s complement whereas it doesn’t in
sign magnitude.

Similarly, you can show that multiplication and subtraction all work in 2’s complement
but do not in other representations. The other number systems require much more
complicated hardware to implement basic mathematical functions. i.e.
add/subtract/multiply.