1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the...
-
Upload
brandon-blankenship -
Category
Documents
-
view
218 -
download
1
Transcript of 1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the...
1
Arithmetic and Logical Operations - Part II
Unsigned Numbers Addition in unsigned numbers is the
same regardless of the base. Given a pair of bit sequences X and Y,
where X = xnxn-1...x1x0
Y = ynyn-1...y1y0
we need to add three terms: xi, yi, and ci for each bit position i = 0...n.
ci is the carry bit in the ith bit position. Assume c0 = 0.
Example 15.1
011 (310)
+) 001 (110)
100 (410)
1+1=10
c1=1
z0Note:ci+1 =(xi+yi+ci)div 2zi =(xi+yi+ci)mod 2
Example 15.2
010010 000111
+) 011000 011001
101010 100000
Overflow in Unsigned Numbers Given a pair of n bit sequences X and Y,
we apply the following check for overflow:
Overflow occurs if
(X + Y) mod 2 X + Y An overflow occurs if a sum is no longer
representable within a fixed number of bits. An incorrect sum is produced when there is a carry out from the most significant position.
Example 15.3
Let the number of bits, n, be 4
1111
0001
10000
also we can check using
(X + Y) mod 2n X + Y
which gives us 0000 10000,
thus overflow occurs.
Carry out of 1 from the most significant
position
Sign Magnitude Integers If two integers in sign magnitude have
the same sign, the two integers are added.
The sign of the result is the same as that of the addends.
The sign bit is not included in the process.
Any carry out from the most significant bit of the magnitude is thrown away.
Example 15.4
1 00100 (-4) 0 00010 (2)
+) 1 00101 (-5) 0 00111 (7)
1 01001 (-9) 0 01001 (9)
0 11111 (31)
0 1 (1)
0100000 (overflow)
A carry outfrom the mostsignificant bithas occurred
If two integers have different signs, a subtraction is performed and the sign of the result is decided in advance. The sign will be that of the integer with the larger magnitude.
The subtraction of unsigned or sign magnitude integers is the same as the longhand subtraction of decimal numbers.
For unsigned numbers, there are results which are not representable.
Example 15.5
0 00101 (5) 1 00010 (-2)
+) 1 00011 (-3) +) 0 00101 (5)
0 00010 (2) 0 00011 (3)
0 00100 (4)
+) 1 00010 (-2)
0 00010 (2)
is now 10becomes 0 borrow
Overflow in Signed Magnitude
If both addends are of the same sign, and there is a carry out from the most significant bit of the magnitude then overflow has occurred.
If the addends have different signs, overflow will not occur.
Two’s Complement In two’s complement, the same
algorithm is applied to the operands regardless of the sign.
Adding two numbers is done by simply applying the same algorithm used for unsigned numbers.
Subtraction can be performed simply by adding the additive inverse of the subtrahend.
Overflow in Two’s complement In two’s complement, overflow does not
necessarily happen when there is a carry out of the most significant bit.
An overflow occurs when both addends are of the same sign and the result is of the opposite sign
If the carry into the most significant bit is not the same as the carry out from the most significant bit an overflow has occurred.
Example 15.6
0101 (5) 0011 (3)
+ 0010 (2) + 1100 (-4)
0111 (7) 1111 (-1)
0101 (5) 1001 (-7)
+ 1101 (-3) + 0111 (7)
10010 (2) 10000 (0)
carry outdiscarded.no overflow
Example 15.7
1111 1000 (-8) 0000 0101 (5)
1111 1000 (-8) 0100 0000 (64)
1111 0000 (-16) 0100 0101 (69)
0111 1110 (126) 1000 0010 (-126)
0110 0000 (96) 1111 1101 (-3)
1101 1110 (-34) 0111 1111 (127)
overflow
Observations
In Example 12.7, an overflow occurs when the numbers have the same signs but the result has a different sign.
If you take both the carry-in of the msb and carry-out from the msb as inputs to an XOR boolean expression, the following holds: if the result is 1 there is an overflow if the result is 0 there is no overflow
One’s Complement
In one’s complement, addition is performed similar to the two’s complement with a slight modification:
The carry out from the most significant
bit is added to the partial sum.
Example 15.8
0011 (+3) 0001 (+1)
1100 (-3) 1001 (-6)
1111 (0) 1010 (+5)
1101 (-2) 0111 (+7)
1011 (-4) 1100 (-3)
11000 10011
1 1
1001 (-6) 0100 (+4)
carry-out isadded to thepartial sum
Multiplication It may take as many as 2n bits to
represent the product of two n-bit numbers
Multiplication in unsigned numbers uses the longhand method we are already familiar with.
By sign extending both the multiplier and the multiplicand to the size needed for the result, the algorithm for multiplying 2’s complement is the same as that of unsigned numbers
Multiplication Algorithm
while (count < no. of integer bits){
check if the multiplier’s last bit is 1
if (multiplier bit = 1){
add the multiplicand to the product}
shift the multiplicand left 1 bit
count = count + 1
shift the multiplier right 1 bit
}
Example 15.90010
x 0011
0000 0000
+ 0010
0000 0010
+ 0100
0000 0110
multiplicand
multiplierinitial product
final product
multiplicandshifted left
If we have n-bit two’s complement integers, we both sign extend the multiplier and the multiplicand to the size needed for the result, i.e., 2n.
By making both the multiplier and the multiplicand 2n the result of the operation will have 4n bits.
The correct product is contained in the least 2n bits of the 4n-bit wide result
Example 15.10Let the number of bits for integers be 4 bits, i.e. n = 4. We sign extend the numbers using 2n bits.
-3 x 6 = ? -3 = 1101 6 = 0110
11111101
00000110
1111101
111101
xxxxxxxx11101110
showed onlyrelevant addends.note: there are only two1’s in the multiplier
discard-18
Example 15.112 x -2 = -4 2 = 0010 -2 = 1110
0000 0010 0000 0000
x 1111 1110 0000 0100
0000 1000
0001 0000
+) 0010 0000
0100 0000
1000 0000
0000 0000
1111 1100 (-4)
Note: each addendis the multiplicandshifted left 1 bit
in each step
Example 15.12-1 x -2 = ? -1 = 1111 -2 = 1110
11111111 (-1)
11111110 (-2)
11111110
11111100
11111000
11110000
11100000
11000000
10000000
00000010 (+2)
Division
Division of unsigned binary numbers is the performed similar to the longhand division of decimal numbers
No convenient algorithm exists for division of signed magnitude and complement integers
An exception that must be handled is division by zero
Division AlgorithmAlign Most Significant Bitsremainder = dividendwhile count < # bits shifted + 1{ remainder = remainder - divisor if (remainder < 0){remainder = remainder + divisorshift left quotient & set lsb to 0}
else shift left quotient & set lsb to 1 shift the divisor right count = count + 1}
Example 15.13What is 31/4? (00011111 / 0100)
C Q D R R-D0 0001 0001 0000 0001 1111 11111 0011 0000 1000 0000 1111 01112 0111 0000 0100 0000 0111 0011
Notice the alignment of the bits for the D. 0100 became 00010000. The D was shifted left twice to be aligned. Count is 2+1=3, so the loop ends here.
Example 12.14What is the result of 99/3? (01100011 / 0011)
C Q D R R-D0 0000 0001 0110 0000 0110 0011 00111 0000 0010 0011 0000 0000 0011 Neg2 0000 0100 0001 1000 0000 0011 Neg3 0000 1000 0000 1100 0000 0011 Neg4 0001 0000 0000 0110 0000 0011 Neg5 0010 0001 0000 0011 0000 0011 0
Notice we get the right answer just as the loop ends from the count.
Dividing Signed Numbers We can use the algorithm to perform
division on the absolute values of the numbers.
Compare the signs of the dividend and the divisor. If they are the same, the quotient is positive. If they are not the same, the quotient is negative.
The sign of the remainder is always the same as the sign of the dividend