Twos Comp
Twos Comp
Twos Comp
00001111
(2) (3)
= =
(1)
00001111 00011100
11110000
11110001
(2)
(3)
00011100
11100011
11100100
To convert a negative twos complement number back to decimal, follow these steps: (1) ip 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 = 00001100 = 00001101 = 13 Here are a few more examples of converting 8-bit twos complement back to decimal (remember that if the number begins with a 0, its a non-negative integer represented in standard binary):
(1) (2) (3)
=
(1)
22
(2) (3)
= =
(1)
01101001 73
01101010
106
(2)
(3)
00110110
00110111
55
Using 8-bit twos complement, one can represent integers in the range 128 (10000000) to 127 (01111111), and 0 is represented in only one way (00000000). Finally, the real utility and power of twos complement is that one can add pairs of twos complement numbers (whether positive or negative) in the usual way, and the result will be the correct answer, in twos complement! In the following examples, superscripts in the binary addition represent carries. + 13 15 28 + 0 0 0 0 0 0 0 0 0 01 0 1 11 1 1 11 1 1 01 1 0 1 1 0
Note that this is just standard binary addition. Now, however, lets consider subtracting 15 from 28; this is equivalent to adding 15 to 28. 28 15 13 + 28 15 13 + 01 1 0 01 1 0 01 1 0 1 1 0 1 0 1 1 0 1 0 0 0 0 1 1
Note that we ignore the carry out of the last column.1 Now consider subtracting 28 from 15. 15 28 13 + 15 28 13 + 0 1 1 0 1 1 0 1 1 01 0 1 11 0 0 1 1 0 1 0 1 1 0 1
Note that the answer obtained is the proper twos complement representation of 13. Finally, consider adding 13 and 15. + 13 15 28 + 11 1 1 11 1 1 11 1 1 1 1 0 0 0 0 01 0 1 11 0 0 1 1 0
Note that we again ignore the carry out of the last column, and we obtain the proper twos complement representation of 28. More examples of twos complement may be found at: http://en.wikipedia.org/wiki/Twos_complement
In general, carries into or out of the most signicant bit must be carefully considered as this may result from an overow condition, i.e., obtaining a result which is too large (> 127) or too small (< 128) to be represented in 8-bit twos complement. In this brief note, however, we assume that all results can be properly represented in 8-bit twos complement.