Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Arithmetic and Logical Operations - Part II

2 Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences X and Y, where X = x n x n-1...x 1 x 0 Y = y n y n-1...y 1 y 0 we need to add three terms: x i, y i, and c i for each bit position i = 0...n. c i is the carry bit in the i th bit position. Assume c 0 = 0.

3 Example 15.1 011(3 10 ) +)001(1 10 ) 100(4 10 ) 1+1=10 c 1 =1 z0z0 Note: c i+1 =(x i +y i +c i )div 2 z i =(x i +y i +c i )mod 2

4 Example 15.2 010010000111 +)011000011001 101010 100000

5 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.

6 Example 15.3 Let the number of bits, n, be 4 1111 0001 10000 also we can check using (X + Y) mod 2 n  X + Y which gives us0000  10000, thus overflow occurs. Carry out of 1 from the most significant position

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

8 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 out from the most significant bit has occurred

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.

10 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 10 becomes 0 borrow

11 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.

12 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.

13 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.

14 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 out discarded. no overflow

15 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

16 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

17 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.

18 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 1001 (-6) 0100 (+4) carry-out is added to the partial sum

19 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

20 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 }

21 Example 15.9 0010 x0011 0000 +0010 0000 0010 +0100 0000 0110 multiplicand multiplier initial product final product multiplicand shifted left

22 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

23 Example 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 = 11016 = 0110 11111101 00000110 1111101 111101 xxxxxxxx11101110 showed only relevant addends. note: there are only two 1’s in the multiplier discard -18

24 Example 15.11 2 x -2 = -42 = 0010-2 = 1110 0000 0010 0000 0000 x 1111 1110 0000 0100 0000 1000 0001 0000 +)0010 0000 0100 0000 1000 0000 0000 1111 1100 (-4) Note: each addend is the multiplicand shifted left 1 bit in each step

25 Example 15.12 -1 x -2 = ?-1 = 1111 -2 = 1110 11111111(-1) 11111110(-2) 11111110 11111100 11111000 11110000 11100000 11000000 10000000 00000010 (+2)

26 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

27 Division Algorithm Align Most Significant Bits remainder = dividend while count < # bits shifted + 1{ remainder = remainder - divisor if (remainder < 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 }

28 Example 15.13 What is 31/4? (00011111 / 0100) CQDRR-D 000010001 00000001 11111111 100110000 10000000 11110111 201110000 01000000 01110011 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.

29 Example 12.14 What is the result of 99/3? (01100011 / 0011) CQDRR-D 00000 00010110 00000110 00110011 10000 00100011 00000000 0011Neg 20000 01000001 10000000 0011Neg 30000 10000000 11000000 0011Neg 40001 00000000 01100000 0011Neg 50010 00010000 00110000 00110 Notice we get the right answer just as the loop ends from the count.

30 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


Download ppt "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."

Similar presentations


Ads by Google