Issue 2 (September 26, 2013)
SUBTRACTION WITH COMPLEMENTS When subtraction is implemented with digital hardware, the subtraction method that uses complements is more efficient than the conventional subtraction method that uses the borrow concept. The same hardware can be used for both addition and subtraction. In the subtraction with complements method, instead of subtracting the subtrahend from the minuend, the complement complement of the subtrahend is added to the minuend. minuend. The sign of the difference is determined according to the existence of the t he end carry. Subtraction with Complements for Unsigned Numbers
The subtraction of two n-digit unsigned numbers M – N in base r can be done as follows: 1. Add the minuend M to the r’s complement of the subtrahend N. Mathematically; n
n
M + (r – N) = M – N + r . n
2. If M N , the sum will produce an end carry r . This end carry will be discarded to obtain the result M – N. The result is positive. n 3. If M < N , the sum does not produce an end carry and is equal to r – (N – M), which is the r ’s ’s complement of (N – M). To obtain the answer in the familiar form, take the r’s complement of the sum and place a negative sign in front .
and N must must have the same number of digits before the summation. If the number Note that M that M and of digits of M and and N are are not the same, zeros must be added to the left of the most significant digit position of the one with the less digits until the number of digits becomes the same.
When subtracting with complements, we recognize the negative answer from the absence of the end carry and the complemented result. To summarize briefly,
To perform the subtraction of unsigned numbers ( M – N ) with r ’s ’s complements proceed as follows: 1. Check the number of digits of M and and N whether whether they are the same. If the number of digits is not the same, add zeros to the left of the most significant digit position of the one with the fewer digits. 2. Obtain the r ’s ’s complement of N . 3. Add the r ’s ’s complement of N to to M . 4. If the summation produces an end carry, this corresponds to the M end carry. The result is a positive number.
N case.
Discard the
5. If the summation does not produce an end carry, this corresponds to the M < N case. case. The result is a negative number. Take the r ’s ’s complement of the sum and place a negative sign in front. Subtraction of unsigned numbers can also be done by means of the ( r – 1)’s complement. Remember that the (r – – 1)’s complement is one less than the r ’s ’s complement. Because of this, the result of adding the minuend to the ( r – – 1)’s complement of the subtrahend produces a sum that is one less than the correct difference when an end carry occurs. Removing the end carry and adding 1 to the sum is referred to as an end-around carry. carry. When the sum does not produce an r – 1)’s complement of the sum end carry, the result is a negative number. In this case, take the ( r and place a negative sign in front.
1/6
Issue 2 (September 26, 2013)
Example (Prob. 1.18 from Mano, 2007):
Perform subtraction on the given unsigned numbers using the 2’s complement of the subtrahend. Where the result should be negative, find its 2’s complement and affix a minus sign. Verify your answer. a) 10011 – 10001 (b) 1001 – 101000 Solutions: a) The 2’s complement of the subtrahend 10001 = 01111
1111 10011 +01111 100010
carry bits
Since the summation produces an end carry the result is positive. Discard the end carry; the result is a positive number. The result of the subtraction: 10011 – 10001 = 00010 Verification of the result: 4
3
2
1
0
Minuend : (10011)2 = 1×2 + 0×2 + 0×2 + 1×2 + 1×2 = 16 + 2 + 1 = (19) 10 4 3 2 1 0 Subtrahend : (10001) 2 = 1×2 + 0×2 + 0×2 + 0×2 + 1×2 = 16 + 1 = (17) 10 4 3 2 1 0 Difference : (00010)2 = 0×2 + 0×2 + 0×2 + 1×2 + 0×2 = 2 = (2) 10 The result of the subtraction in decimal number system is 19 – 17 = 2. Thus, the result is verified. b) Here, the minuend (1001) has 4 digits and the subtrahend (101000) has 6 digits. At first we have to equate the number of digits of the minuend and the subtrahend. Therefore, we add two zeros the left of the most significant number of the minuend and make it (001001). The 2’s complement of the subtrahend 101000 = 011000 11 001001 +011000 100001
carry bits
Since the summation does not produce an end carry the result is a negative number. Take the 2’s complement of the sum and place a negative sign in front. The 2’s complement of the result (100001) is 011111 The result of the subtraction: 1001 – 101000 = – 011111 Verification of the result: 5
4
3
2
1
0
Minuend : (001001)2 = 0×2 + 0×2 + 1×2 + 0×2 + 0×2 + 1×2 = 8 + 1 = (9) 10 5 4 3 2 1 0 Subtrahend : (101000)2 = 1×2 + 0×2 + 1×2 + 0×2 + 0×2 + 0×2 = 32 + 8 = (40) 10 5 4 3 2 1 0 Difference : – (011111)2 = – (0×2 +1×2 + 1×2 + 1×2 + 1×2 + 1×2 ) = – (16 + 8 + 4 + 2 + 1) = – (31) 10 The result of the subtraction in decimal number system is 9 – 40 = –31. Thus, the result is verified.
2/6
Issue 2 (September 26, 2013)
SIGNED BINARY NUMBERS In ordinary arithmetic, a negative number is indicated by a minus sign and a positive number by a plus sign. Because of hardware limitations, computers must represent everything with binary digits. The Signed-Magnitude Representation
In this system, the number consists of a magnitude and a symbol (+ or –) or a bit (0 or 1).
The sign bit is placed in the leftmost position of the number.
The convention is to make sing bit 0 for positive and 1 for negative.
For example, consider the bit string 01001:
o
For unsigned binary, 01001 is considered as 9.
o
For signed binary, 01001 is considered as +9 (because the leftmost bit is 0).
For example, consider the bit string 11001: o
For unsigned binary, 11001 is considered as 25.
o
For signed binary, 11001 is considered as –9 (because the leftmost bit is 1).
In order to avoid confusion, the type of representation for the number should be known in advanced.
Subtraction with Signed-Magnitude Numbers
To perform the subtraction of signed numbers ( M – N ) with r ’s complements proceed as follows: 1. The number of digits of M and N should be the same. 2. Obtain the r ’s complement of N (including the sign bit). 3. Add the r ’s complement of N to M (including the sign bit). 4. If the summation produces an end carry. Discard the end carry. After the end carry is discarded, the leftmost bit is the sign bit. -
If the sign bit is 0, the result is positive,
-
If the sign bit is 1, the result is negative.
5. If the summation does not produce an end carry, the leftmost bit is the sign bit. 6. The same procedure can be applied to the ( r – 1)’s complement system by including the end-around carry as it is done with unsigned numbers. Signed-Complement Representation (for Binary Numbers)
In this system, a negative number is always indicated by its complement.
This system is more convenient when arithmetic operations are implemented in a computer.
The signed-complement system negates a number by taking its complement including the sign bit.
The complement of the positive number is taken including the sign bit.
Since positive numbers always start with 0 (plus) in the leftmost position, the complement will always start with a 1, indicating a negative number.
The signed-complement system can use either the 1’s or the 2’s complement, but the 2’s complement is the most common. 3/6
Issue 2 (September 26, 2013)
As an example, consider the number 9, represented in binary with eight bits. o
There is only one way to represent +9
: 00001001
o
There are three different ways to represent –9 with eight digits:
Signed-magnitude representation of –9
: 10001001
Signed-1’s complement representation –9 : 1 1 1 1 0 1 1 0
Signed-2’s complement representation –9 : 1 1 1 1 0 1 1 1 minus sign
Note that all negative numbers have a 1 in the left most bit position. Thus, we can distinguish them from the positive numbers.
1’s complement system is useful as a logical operation, since the change of 1 to 0 or 0 to 1 is equivalent to a logical complement operation, as will be shown in the Boolean algebra chapter.
The same procedure can be applied to the 1’s complement system by including the end-around carry as it is done with unsigned numbers.
Addition and Subtraction in Signed-2’s Complement System •
In the signed-2’s complement system, - Positive binary numbers (start with 0) are represented in the signed-magnitude form. - Negative binary numbers (start with 1) are represented in the signed-2’s complement form.
•
The same procedure is applied for the addition and subtraction of numbers M and N : 1. The number of digits of M and N should be the same. 2. Negative numbers must initially be in the signed 2’s complement form. 3. Perform the operation M + N 4. Discard the end carry bit (if any). Then, the leftmost bit is the sign bit. -
If the sign bit is 0, the result is positive.
-
If the sign bit is 1, the result is negative and in 2’s complement form. Take the 2’s complement of the result and place a minus sign in front.
This procedure is adopted because a subtraction operation can be changed to an addition operation if the sign of the subtrahend is changed as shown in the following relationship: (±A) – (+B) = (±A) + (–B); (±A) – (–B) = (±A) + (+B). Changing a positive number to a negative number is easily done by taking the 2’s complements of the positive number. Reverse is also true, because the complement of a negative number in complement form produces the equivalent positive number.
4/6
Issue 2 (September 26, 2013)
Example (Prob. 1.20 from Mano 2007) Convert decimal +49 and +29 to binary, using the signed-2’s complement representation and enough digits to accommodate the numbers. Then perform the binary equivalent of following. Convert the numbers back to decimal and verify that they are correct.
a) (+29) + (–49) b) (–29) + (+49) c) (–29) + (–49) Solution: Conversion of 49 from decimal to binary:
Integer Integer Dividend Divider Quotient Remainder 49 2 24 1 (LSB) 24 2 12 0 12 2 6 0 6 2 3 0 3 2 1 1 1 2 0 1 (MSB)
(49)10 = (1 1 0 0 0 1) 2
Conversion of 29 from decimal to binary: Integer Integer Dividend Divider Quotient Remainder 29 2 14 1 (LSB) 14 2 7 0 7 2 3 1 3 2 1 1 1 2 0 1 (MSB)
(29)10 = (1 1 1 0 1) 2
In order to equate the number of bits of both binary numbers we add a zero to the most significant bit (MSB) position of the binary equivalent of decimal 29, and obtain (29)10 = (0 1 1 1 0 1) 2 Let’s express the positive binary numbers by placing a zero (0) to the left of the MSBs. (+49)10 = (0 1 1 0 0 0 1) 2
;
(+29)10 = (0 0 1 1 1 0 1) 2
Sign bits (0: Positive) Let’ take the 2’s complement of these binary numbers to obtain the corresponding negative numbers. (–49)10 = 2’s complement of (0 1 1 0 0 0 1) = [1’s complement (0 1 1 0 0 0 1) + 1] = (1 0 0 1 1 1 0 + 1) 2 = (1 0 0 1 1 1 1) 2 (–29)10 = 2’s complement of (0 0 1 1 1 0 1) = [1’s complement (0 0 1 1 1 0 1) + 1] = (1 1 0 0 0 1 0 + 1) 2 = (1 1 0 0 0 1 1) 2 a)
(+29)10 + (–49)10 = (0 0 1 1 1 0 1) 2 + (1 0 0 1 1 1 1) 2 = ? +29 –49 –20
1 0 0 + 1 0 1 1
1 1 0 0
1 1 1 1
1 1 1 1
1 0 1 1 1 0 0
carry bits
(There is no end carry and the sign bit is 1) 5/6
Issue 2 (September 26, 2013)
Since there is no end carry and the sign bit is 1, the result is negative. This means that the result is in 2’s complement form. We take the 2’s complement of the result and place a minus sign in front as follows: 4
2
2’s complement of ( 1 1 0 1 1 0 0 ) 2 = (0 0 1 0 1 0 0) 2 = (1×2 + 1×2 ) = (20)10 Leave unchanged Replace 1’s with 0’s and 0’s with 1’s. b)
(–29)10 + (+49)10 = (1 1 0 0 0 1 1) 2 + (0 1 1 0 0 0 1) 2 = ?
–29 +49 +20
1 11 1100011 +0110001 10010100
carry bits
4
2
+(0 1 0 1 0 0) 2 = +(1×2 + 1×2 ) = + (20)10
Sign bit is 0, the sign is positive. The result is not in complement form. Remove the end carry.
c)
(–29)10 + (–49)10 = (1 1 0 0 0 1 1) 2 + (1 0 0 1 1 1 1) 2 = ? To accommodate the overflow values we must increase the word size by 1. Add 1 bit to the left of the sign bits. This is called sign extension.
–29 –49 –78
11 1111 11100011 + 11001111 110110010
carry bits
(1 0 1 1 0 0 1 0 ) 2
Sign bit is 1, the sign is negative. The result is in complement form. Remove the last carry. 6
3
2
1
The 2’s complement of (1 0 1 1 0 0 1 0 )2 = – (0 1 0 0 1 1 1 0) 2 = – (1×2 + 1×2 + 1×2 + 1×2 ) Leave unchanged Replace 1’s with 0’s and 0’s with 1’s. Thus, the result is (–29)10 + (–49)10 = (1 1 0 0 0 1 1) 2 + (1 0 0 1 1 1 1) 2 = – (0 1 0 0 1 1 1 0) 2 6
3
2
1
= – (1×2 + 1×2 + 1×2 + 1×2 )10 = – (78)10
Note: In signed 2’s complement system, when the minuend and the subtrahend are both negative
numbers we have to make sign extension. Otherwise, after removing the end carry from the result. 0 bit is left as the sign bit which is wrong.
6/6