Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Twos Comp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSU200 Discrete Structures Fall 2005

Profs. Aslam & Fell October 27, 2005

Representing Negative Numbers: Twos Complement


In class we have discussed binary representations of the non-negative integers {0, 1, 2, . . .}. However, 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-signicant bit of 0 represents + while a most-signicant 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 twos complement. In twos 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) ip all the bits, and (3) add 1. For example, the 8-bit twos complement representation of 13 is 00001101 (as before) while 13 is represented as follows (using the steps given above): 13 = 00001101 = 11110010 = 11110011 Note that a most-signicant bit of 1 again signies 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 twos complement form (remember that non-negative integers are represented in standard binary): 15 15 28 28 =
(1) (1) (2) (3)

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)

00010110 10010110 01001001 11001001

=
(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.

You might also like