Presentation is loading. Please wait.

Presentation is loading. Please wait.

08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic.

Similar presentations


Presentation on theme: "08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic."— Presentation transcript:

1 08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic

2 08 ARTH Page 2 ECEn/CS 224 Unsigned Addition

3 08 ARTH Page 3 ECEn/CS 224 Binary Addition 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 and carry 1 to the next column Examples: 0101 (5 10 ) + 0010 (2 10 ) 0111 (7 10 ) Carries 1 1 1 0101 (5 10 ) + 0011 (3 10 ) 1000 (8 10 )

4 08 ARTH Page 4 ECEn/CS 224 Binary Addition w/Overflow Add 45 10 and 44 10 in binary Carries 1 1 1 101101 (45 10 ) + 101100 (44 10 ) 1011001 (89 10 ) If the operands are unsigned, you can use the final carry-out as the MSB of the result. Adding 2 k bit numbers  k+1 bit result

5 08 ARTH Page 5 ECEn/CS 224 More Binary Addition w/Overflow 1 1 1 1 1111 (15 10 ) + 0001 (1 10 ) 0000 (0 10 ) If you don’t want a 5-bit result, just keep the lower 4 bits. This is insufficient to hold the result (16). It rolls over back to 0.

6 08 ARTH Page 6 ECEn/CS 224 Signed Numbers

7 08 ARTH Page 7 ECEn/CS 224 Negative Binary Numbers Several ways of representing negative numbers Most obvious is to add a sign (+ or -) to the binary integer

8 08 ARTH Page 8 ECEn/CS 224 Sign-Magnitude 0 is + 1 is - Easy to interpret number value

9 08 ARTH Page 9 ECEn/CS 224 Sign-Magnitude Examples 0 0 0 0 0011 (+3 10 ) + 0 0100 (+4 10 ) 0 0111 (+7 10 ) Signs are the same Just add the magnitudes 0 0 0 1 0011 (-3 10 ) + 1 0100 (-4 10 ) 1 0111 (-7 10 )

10 08 ARTH Page 10 ECEn/CS 224 Another Sign-Magnitude Example 0 0101 (+5 10 ) + 1 0011 (-3 10 ) Signs are different - determine which is larger magnitude Put larger magnitude number on top Subtract Result has sign of larger magnitude number… 0 0101 (+5 10 ) - 1 0011 (-3 10 ) 0 0010 (+2 10 )

11 08 ARTH Page 11 ECEn/CS 224 Yet Another Sign-Magnitude Example 0 0010 (+2 10 ) + 1 0101 (-5 10 ) Signs are different - determine which is larger magnitude Put larger magnitude number on top Subtract Result has sign of larger magnitude number… 1 0101 (-5 10 ) - 0 0010 (+2 10 ) 1 0011 (-3 10 )

12 08 ARTH Page 12 ECEn/CS 224 Sign-Magnitude Addition requires two separate operations –addition –subtraction Several decisions: –Signs same or different? –Which operand is larger? –What is sign of final result? Two zeroes (+0, -0)

13 08 ARTH Page 13 ECEn/CS 224 Sign-Magnitude Advantages –Easy to understand Disadvantages –Two different 0s –Hard to implement in logic

14 08 ARTH Page 14 ECEn/CS 224 One’s Complement Positive numbers are the same as sign- magnitude -N is represented as the complement of N + -N = N’

15 08 ARTH Page 15 ECEn/CS 224 One’s Complement Examples 000101 (5 10 ) + 010100 (20 10 ) 011001 (25 10 ) 1 1 0 0 0101 (5 10 ) + 1100 (-3 10 ) 0001 0010 (+2 10 ) + If there is a carry out on the left, it must be wrapped around and added back in on the right

16 08 ARTH Page 16 ECEn/CS 224 One’s Complement Addition complicated by end around carry No decisions (like SM) Still two zeroes (+0, -0)

17 08 ARTH Page 17 ECEn/CS 224 One’s Complement Advantages –Easy to generate –N –Only one addition process Disadvantages –End around carry –Two different 0s

18 08 ARTH Page 18 ECEn/CS 224 Two’s Complement Treat positional digits differently 0111 2C = -0 x 2 3 + 1 x 2 2 + 1 x 2 1 + 1 x 2 0 = 7 10 1111 2C = -1 x 2 3 + 1 x 2 2 + 1 x 2 1 + 1 x 2 0 = -1 10 Most significant bit (MSB) given negative weight… Other bits same as in unsigned

19 08 ARTH Page 19 ECEn/CS 224 Two’s Complement

20 08 ARTH Page 20 ECEn/CS 224 Sign-Extension in 2’s Complement To make a k-bit number wider –replicate sign bit 110 2C = -1 x 2 2 + 1 x 2 1 = -2 10 1110 2C = -1 x 2 3 + 1 x 2 2 + 1 x 2 1 = -2 10 11110 2C = -1 x 2 4 + 1 x 2 3 + 1 x 2 2 + 1 x 2 1 = -2 10

21 08 ARTH Page 21 ECEn/CS 224 More Sign-Extension 010 2C = 1 x 2 1 = 2 10 0010 2C = 1 x 2 1 = 2 10 00010 2C = 1 x 2 1 = 2 10 Works for both positive and negative numbers

22 08 ARTH Page 22 ECEn/CS 224 Negating a 2’s Complement Number Invert all the bits Add 1 +2 10 = 0010 2C → 1101 + 1 = 1110 2C = -2 10 invert -2 10 = 1110 2C → 0001 + 1 = 0010 2C = +2 10 invert

23 08 ARTH Page 23 ECEn/CS 224 Two’s Complement Addition 0 0 1 0101 (+5 10 ) + 0001 (+1 10 ) 0110 (+6 10 ) 1 1 1 1011 (-5 10 ) + 1111 (-1 10 ) 1010 (-6 10 ) 1 1 1 0110 (+6 10 ) + 1111 (-1 10 ) 0101 (+5 10 ) Operation is same as for unsigned Same rules, same procedure Interpretation of operands and results are different

24 08 ARTH Page 24 ECEn/CS 224 Two’s Complement Addition always the same Only 1 zero Representation of choice…

25 08 ARTH Page 25 ECEn/CS 224 Two’s Complement Overflow All representations can overflow –Focus on 2’s complement here 1 0 0 0 1011 (-5 10 ) + 1100 (-4 10 ) 0111 (+7 10 ) ?? The correct answer (-9) cannot be represented by 4 bits The correct answer is 10111

26 08 ARTH Page 26 ECEn/CS 224 Overflow Can you use leftmost carry-out as new MSB? The answer is no, in general 1 0 0 0 1011 (-5 10 ) + 1100 (-4 10 ) 10111 (-9 10 ) Works here… 0 0 0 0 1000 (-8 10 ) + 0111 (+7 10 ) 01111 (+15 10 ) ?? Does NOT work here…

27 08 ARTH Page 27 ECEn/CS 224 Handling Overflow 1.Sign-extend the operands 2.Do the addition 0 0 0 0 11000 (-8 10 ) +00111 (+7 10 ) 11111 (-1 10 )

28 08 ARTH Page 28 ECEn/CS 224 When Can Overflow Occur? Adding two positive numbers - yes Adding two negative numbers – yes Adding a positive to a negative – no Adding a negative to a positive - no

29 08 ARTH Page 29 ECEn/CS 224 General Handling of Overflow Adding two k bit numbers gives k+1 bit result Two options include: 1.Sign extend and keep entire k+1 bit result 2.Throw away new bit generated (keep only k bits) i.Detect overflow and signal an error ii.Ignore the overflow (this is what most computers do) The choice is up to the designer

30 08 ARTH Page 30 ECEn/CS 224 Detecting Overflow Overflow occurs if: –Adding two positive numbers gives what looks like a negative result –Adding two negative numbers gives what looks like a positive result Overflow will never occur when adding positive to negative There are other ways of detecting overflow vs. what is suggested here…

31 08 ARTH Page 31 ECEn/CS 224 Arithmetic Circuits

32 08 ARTH Page 32 ECEn/CS 224 Binary Adder An example of an iterative network Full Adder A0 S0 C0 S1S2 C3... B0 C1 A1B1 Full Adder C2 Full Adder A2B2 Sn-1 Cn Cn-1 Full Adder An-1Bn-1 This type of adder is also called a ripple-carry adder because the carry ripples through from cell to cell

33 08 ARTH Page 33 ECEn/CS 224 Full Adder Derivation Si Ci+1 Ai Bi Ci Ai Bi Ci Ai Ci Si Ci+1 Ci Full Adder AiBi

34 08 ARTH Page 34 ECEn/CS 224 Binary Subtracter Full Sub. X1 D1 B1 D2... Y1 B2 X2Y2 Full Sub. B3 Full Sub. Dn-1 Bn Bn-1 Full Sub. Xn-1Yn-1 D (ifference) = X - Y B is the borrow signal Equations generated similarly to full adder B0 D0 X0Y0

35 08 ARTH Page 35 ECEn/CS 224 Another Way to Make a Subtracter (2’s complement) Full Adder Full Adder Full Adder Full Adder C 0 = 1 A0A0 B0B0 B1B1 A1A1 B2B2 A2A2 B3B3 A3A3 B0’B0’B1’B1’B2’B2’B3’B3’ S3S3 C4C4 S2S2 C3C3 S1S1 C2C2 S0S0 S = A + (-B) -B is generated by inverting and adding 1 adding 1 C1C1 inverting

36 08 ARTH Page 36 ECEn/CS 224 Binary Arithmetic Comparison

37 08 ARTH Page 37 ECEn/CS 224 Summary Understand the three main representation of binary integers –Sign-Magnitude –One’s Complement –Two’s Complement Design binary adders and subtracters for each representation


Download ppt "08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic."

Similar presentations


Ads by Google