Download presentation
Presentation is loading. Please wait.
Published byrobinson zapata Modified over 5 years ago
1
Today Binary addition Representing negative numbers 2
2
3 Binary Addition Consider the following binary numbers: 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 How do we add these numbers?
3
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 1 4
4
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 1 And we have a carry now! 5
5
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 0 1 And we have a carry again! 6
6
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 0 0 1 and again! 7
7
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 1 0 0 0 1 8
8
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 1 One more carry! 9
9
Binary Addition 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 1 0 1 0 0 0 1 10
10
11 Binary Addition Behaves just like addition in decimal, but: We carry to the next digit any time the sum of the digits is 2 (decimal) or greater
11
21 Negative Numbers So far we have only talked about representing non-negative integers What can we add to our binary representation that will allow this?
12
22 Representing Negative Numbers One possibility: Add an extra bit that indicates the sign of the number We call this the “sign-magnitude” representation
13
23 Sign Magnitude Representation +1200 0 0 1 1 0 0
14
24 Sign Magnitude Representation +1200 0 0 1 1 0 0 -1210 0 0 1 1 0 0
15
25 Sign Magnitude Representation +1200 0 0 1 1 0 0 -1210 0 0 1 1 0 0 What is the problem with this approach?
16
26 Sign Magnitude Representation What is the problem with this approach? Some of the arithmetic operators that we have already developed do not do the right thing
17
27 Sign Magnitude Representation Operator problems: For example, we have already designed a counter (that implements an ‘increment’ operation) -1210 0 0 1 1 0 0
18
Sign Magnitude Representation Operator problems: -1210 0 0 1 1 0 0 Increment 28
19
Sign Magnitude Representation Operator problems: -121 10 0 0 1 1 0 1 0 0 0 1 1 0 0 Increment 29
20
Sign Magnitude Representation Operator problems: -121 10 0 0 1 1 0 1 0 0 0 1 1 0 0 Increment -13 !!!! 30
21
31 Representing Negative Numbers An alternative: When taking the additive inverse of a number, invert all of the individual bits The leftmost bit still determines the sign of the number
22
One’s Complement Representation -1211 1 1 0 0 1 1 1200 0 0 1 1 0 0 Invert 32
23
One’s Complement Representation -1211 1 1 0 0 1 1 1200 0 0 1 1 0 0 Invert Increment 11 1 1 0 1 0 0 33
24
One’s Complement Representation -1211 1 1 0 0 1 1 -1111 1 1 0 1 0 0 1200 0 0 1 1 0 0 Invert Increment 34
25
35 One’s Complement Representation What problems still exist?
26
36 One’s Complement Representation What problems still exist? We have two distinct representations of ‘zero’: 00 0 0 0 0 0 0 10 0 0 0 0 0 0
27
37 One’s Complement Representation What problems still exist? We can’t directly add a positive and a negative number: 12 + -5 00 0 0 1 1 0 0 + 11 1 1 1 0 1 0
28
One’s Complement Representation 12 + 00 0 0 1 1 0 0 + -511 1 1 1 0 1 0 00 0 0 0 1 1 0 38
29
One’s Complement Representation 12 + 00 0 0 1 1 0 0 + -511 1 1 1 0 1 0 600 0 0 0 1 1 0 !!!! 39
30
Representing Negative Numbers An alternative: (a little intuition first) 000 0 0 0 0 0 0 Decrement 40
31
Representing Negative Numbers An alternative: (a little intuition first) 000 0 0 0 0 0 0 Decrement 11 1 1 1 1 1 1 41
32
Representing Negative Numbers An alternative: (a little intuition first) 000 0 0 0 0 0 0 11 1 1 1 1 1 1 Decrement Define this as 42
33
46 Representing Negative Numbers A few more numbers: 300 0 0 0 0 1 1 200 0 0 0 0 1 0 100 0 0 0 0 0 1 000 0 0 0 0 0 0 11 1 1 1 1 1 1 -211 1 1 1 1 1 0 -311 1 1 1 1 0 1
34
47 Two’s Complement Representation In general, how do we take the additive inverse of a binary number?
35
48 Two’s Complement Representation In general, how do we take the additive inverse of a binary number? Invert each bit and then add ‘1’
36
Two’s Complement Representation -511 1 1 1 0 1 1 Invert each bit and then add ‘1’ 500 0 0 0 1 0 1 Two’s complement 49
37
50 Two’s Complement Representation Now: let’s try adding a positive and a negative number: 12 + -5 00 0 0 1 1 0 0 + 11 1 1 1 0 1 1
38
Two’s Complement Representation Now: let’s try adding a positive and a negative number: 12 + 00 0 0 1 1 0 0 + -511 1 1 1 0 1 1 00 0 0 0 1 1 1 51
39
Two’s Complement Representation Now: let’s try adding a positive and a negative number: 12 + 00 0 0 1 1 0 0 + -511 1 1 1 0 1 1 700 0 0 0 1 1 1 52
40
53 Two’s Complement Representation Two’s complement is used for integer representation in today’s processors
41
54 Two’s Complement Representation Two’s complement is used for integer representation in today’s processors One oddity: we can represent one more negative number than we can positive numbers
42
55 Implementing Subtraction How do we implement a ‘subtraction’ operator? (e.g.,A – B)
43
56 Implementing Subtraction How do we implement a ‘subtraction’ operator? (e.g.,A – B) Take the 2s complement of B Then add this number to A
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.