1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the...

30
1 Arithmetic and Logical Operations - Part II

Transcript of 1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the...

Page 1: 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.

1

Arithmetic and Logical Operations - Part II

Page 2: 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.

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.

Page 3: 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.

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

Page 4: 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.

Example 15.2

010010 000111

+) 011000 011001

101010 100000

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

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.

Page 6: 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.

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

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

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.

Page 8: 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.

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

Page 9: 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.

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.

Page 10: 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.

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

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

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.

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

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.

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

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.

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

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

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

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.

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

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

}

Page 21: 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.

Example 15.90010

x 0011

0000 0000

+ 0010

0000 0010

+ 0100

0000 0110

multiplicand

multiplierinitial product

final product

multiplicandshifted left

Page 22: 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.

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

Page 23: 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.

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

Page 24: 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.

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

Page 25: 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.

Example 15.12-1 x -2 = ? -1 = 1111 -2 = 1110

11111111 (-1)

11111110 (-2)

11111110

11111100

11111000

11110000

11100000

11000000

10000000

00000010 (+2)

Page 26: 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.

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

Page 27: 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.

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}

Page 28: 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.

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.

Page 29: 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.

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.

Page 30: 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.

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