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):