Title: Arithmetic and Logical Operations Part II
1Arithmetic and Logical Operations - Part II
2Unsigned 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.
3Example 15.1
-
- 011 (310)
- ) 001 (110)
- 100 (410)
Note ci1 (xiyici)div 2 zi (xiyici)mod 2
4Example 15.2
- 010010 000111
- ) 011000 011001
-
100000
101010
5Overflow 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.
6Example 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.
-
7Sign 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.
8Example 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)
9- 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.
10Example 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)
11Overflow 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.
12Twos Complement
- In twos 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.
13Overflow in Twos complement
- In twos 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.
14Example 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)
-
15Example 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)
16Observations
- 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
17Ones Complement
- In ones complement, addition is performed
similar to the twos complement with a slight
modification - The carry out from the most significant
- bit is added to the partial sum.
18Example 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 is added to the partial sum
19Multiplication
- 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 2s complement is
the same as that of unsigned numbers
20Multiplication Algorithm
- while (count lt no. of integer bits)
- check if the multipliers 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
21Example 15.9
multiplicand
- 0010
- x 0011
-
- 0000 0000
- 0010
- 0000 0010
- 0100
- 0000 0110
-
multiplier
initial product
multiplicand shifted left
final product
22- If we have n-bit twos 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
23Example 15.10
- Let 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
-18
24Example 15.11
- 2 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 addend is the multiplicand shifted
left 1 bit in each step
25Example 15.12
- -1 x -2 ? -1 1111 -2 1110
- 11111111 (-1)
- 11111110 (-2)
- 11111110
- 11111100
- 11111000
- 11110000
- 11100000
- 11000000
- 10000000
- 00000010 (2)
26Division
- 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
27Division Algorithm
- Align Most Significant Bits
- remainder dividend
- while count lt bits shifted 1
- remainder remainder - divisor
- if (remainder lt 0)
- remainder remainder divisor
- shift left quotient set lsb to 0
- else
- shift left quotient set lsb to 1
-
- shift the divisor right
- count count 1
-
28Example 15.13
What is 31/4? (00011111 / 0100) C Q D R R-D 0
0001 0001 0000 0001 1111 1111 1 0011 0000
1000 0000 1111 0111 2 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 213, so the
loop ends here.
29Example 12.14
- What is the result of 99/3? (01100011 / 0011)
- C Q D R R-D
- 0 0000 0001 0110 0000 0110 0011 0011
- 1 0000 0010 0011 0000 0000 0011 Neg
- 2 0000 0100 0001 1000 0000 0011 Neg
- 0000 1000 0000 1100 0000 0011 Neg
- 0001 0000 0000 0110 0000 0011 Neg
- 5 0010 0001 0000 0011 0000 0011 0
- Notice we get the right answer just as the loop
ends from the count.
30Dividing 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