In considering arithmetic in the computer, we must be able to represent negative integers. Several methods have been used for expressing negative numbers in the computer. The most obvious way is to convert the number to binary and stick on another bit to indicate sign, 0 for positive and 1 for negative. Suppose integers are stored using this signed-magnitude technique in 8 bits so that the leftmost or most significant bit holds the sign while the remaining bits represent the magnitude. Consequently, 0010 10012 represents +4110.


| Early computers used signed-magnitude, but the circuitry for arithmetic was complicated, and the mapping from the signed-magnitude representation to decimal integers was not one-to-one. For example, representing zero as all zero's, (0000 0000) seems natural; but that represents +0. The bit string 1000 0000 indicates -0. Thus, to determine if a variable had a value of zero, the programmer had to check if the variable was +0 or -0 |
| Definition | The signed-magnitude representation of signed integers expresses the number in binary in a fixed number of bits; and the most significant (leftmost) bit indicates the sign, 0 for positive and 1 for negative. |

| Definition | The complement of a bit is its opposite value, 0 or 1. The one's complement of binary number is the number with each bit complemented. |
| 1111 1111 |
| - 0010 1001 |
| 1101 0110 |
| Algorithm
2.1 Algorithm for Computing the Two's
Complement of a Binary Integer: find the one's complement by complementing each bit, 0 <-> 1 increment the result by 1 |
Example 2.1
Let us go through this process to express the decimal
integers 104 and -104 in 8 bits with two's complement representation. The
initial step is to convert 104 to binary. ![]() We place a zero bit at the beginning to show the sign (positive) for the 8-bit number and obtain 10410 = 0110 10002. Since 104 is positive, we are through for the first number. With -104, we complement each bit and then add 1. ![]() Thus, -10410 = 1001 1000. If the computer stores integers using 16 bits, we take the two's complement of 0000 0000 0110 10002, yielding 1111 1111 1001 10002. ![]() |
| Example 2.2
If a computer uses 8-bit, two's complement representation
for integers, let us determine what decimal numbers the following two's
complement integers represent. a. 0000 0101 b. 1111 1011 c.
0111 1111 d. 1000 0000 e. 1111 1111 a. The leading 0 indicates that 0000 0101 is a positive integer, so we can determine immediately that it represents 4 + 1 = +5. b. With the most significant bit being 1, 1111 1011 represents a negative integer. To determine the magnitude, we take the two's complement by complementing all bits up to but not including the rightmost 1 (or by subtracting the number from 1111 1111 and then adding 1)--- 0000 0101. Consequently, 1111 1011 is the representation for -5. c. 0111 1111 represents a positive integer that is one less than the unsigned binary integer 1000 0000 = 12810. Thus, our original number is 127. d. 1000 0000 represents a negative integer. To determine the magnitude of the number, we determine the decimal equivalent of its two's complement. The two's complement of this number, however, is also 1000 0000, which represents 128 in base 2. Thus, the sequence of bits in two's complement notation represents -12810. Moreover, this number is the smallest integer that can be expressed in 8-bit, two's complement representation. Thus, the range of 8-bit signed integers is from -12810 to 2710. We have one more negative integer than positive. e. The two's complement of 1111 11112 is 0000 00012, indicating that the original number represents the largest negative integer, -110. |
| Example 2.3
Following the same procedure as in Parts c and d of the
last example, let us find the range of the decimal integers using 16-bit,
two's complement representation. The largest positive integer is which is one less than the unsigned integer 1000 0000 0000 0000 = 215 = 32,768. The smallest negative integer is 1000 0000 0000 0000 in two's complement notation. As in Part d above, this integer is its own two's complement. Therefore, -32,768 is the smallest negative integer. Many microcomputers store integers in 16 bits, so that the range of number of type int is from -32,768 to 32,767. |
| Quick Review Questions |
|
|
| Base 10 | Base 2 |
![]() |
| 100111 + 1 | = (25 + 22 + 2 + 1)10 + 1 |
| = 25 + 22 + 2 + 2 | |
| = 25 + 22 + (2)(2) | |
| = 25 + 22 + 22 | |
| = 25 + (2)(22) | |
| = 25 + 23 = 1010002 |

| Unsigned | Signed | |
| 1011 1110 = | 190 | -66 |
| +0010 1101 = | + 45 | + 45 |
| 1110 1011 = | 235 | -21 |
| Quick Review Question | |||||
|
Example 2.4
Let us perform the following subtraction using 8-bit,
two's complement integers.
|
| i = 20480; | |
| i = i + 16384; | |
| cout << i << endl; |
| 0101 0000 0000 0000 = | 20,480 |
| + 0100 0000 0000 0000 = | + 16,384 |
| 1001 0000 0000 00002 = | -28,672 |
| 1000 0000 = | -128 |
| + 1000 0000 = | -128 |
| 0000 0000 = | 0 |
| Definition | Overflow is an error condition that occurs when there are not enough bits to express a value. |
| 1. 115 | 2. -37 | 3. -64 |
| 4. -151 | 5. 128 | 6. -6 |
| 9. 0011 1111, 1100 0000 | 10. 1111 1111, 1001 0011 |
| 11. 11 1110 1111 + 10 1101 | 12. 1111 + 1001 + 1 1011 + 101 |
| 13. 1001 1100 + 0111 0100 |
14. 1110 0000 + 1001 1100 |
15. 0011 0110 - 1111 0110 |
Copyright © 2002 -
, Dr. Angela B. Shiflet
All rights reserved