Download presentation
Presentation is loading. Please wait.
Published bySiska Kusumo Modified over 5 years ago
1
Integer arithmetic Depends what you mean by "integer"
Assume at 3-bit string. Then we define zero = 000 one = 001 Use zero, one and binary addition: Zero One 001 Zero + one = one. Makes sense!
2
Add one repeatedly, use up all possible patterns:
Zero Called the Unsigned Integer System No negative integers!
3
Two additions: 5 9
4
Two additions: 2 010 + 3 + 011 5 101 Yes! 5 = 101 4 100 + 5 + 101
Yes! 5 = 101 9 001; 001 represents one. is = 1???
5
Addition of unsigned integers
Error detected by presence of "carry"
6
How do we subtract unsigned integers?
We need the concept of the "Two's complement"
7
One's complement Take any string Invert every bit
This is One's complement "NOT"
8
Two's complement Given a string One's complement then add one
This is called two's complement
9
To subtract unsigned A-B
Perform A + 2's comp (B) = A + Not (B) + 1
10
Example: 010 110 =2; Good! Carry! =6; BAD! No Carry!
11
Subraction of unsigned integers
Error detected by absence of carry! Warning: Some machines invert the carry bit on subtraction So that "carry" => Error for both add and sub
12
Conclusion For unsigned arithmetic we are interested in carry
Pay attention! I never used the word "overflow" thats something completely different. Also notice: 3-bit operands gave 3-bit results. Don't be tempted to write that 4'th bit down!
13
How about negative numbers?
How should we represent -1? How would we compute 0 - 1? 0 + 2's compl (1) We choose this as our "-1" 1 = 001 - 1 = 110 + 1 111
14
Repeatedly add -1: Zero 000 - 1 111 - 2 110 Less than - 3 101 zero
Less than zero No! High order bit called "sign bit"
15
Signed 3-bit integers 3 011 2 010 1 001 0 000 -1 111 -2 110 -3 101
Not symmetrical around zero!!!
16
Sign bit The high order bit in a number Also called "N"-bit
Value is negative when this bit is "1"
17
Let's try A + B (-1) 111 *000 Both results is OK But: Left case: no carry Right case: carry Conclusion: For signed addition carry is worthless Same conclusion for signed subtraction * carry
18
Some additions A +(-3) (-4) 1 0 0 +(-2)
19
Some additions B +(-3) (-4) 1 0 0 -4 C C +(-2)
20
Some additions C OK BAD +(-3) (-4) 1 0 0 -4 C C +(-2) OK OK
21
Some additions D OK BAD +(-3) (-4) 1 0 0 -4 C C +(-2) OK OK
22
Error during signed addition:
R = A + B A, B same sign and R opposite sign called overflow Notice: Matematically, signed addition is the same as unsigned addition The same is true for signed subtraction and unsigned subtraction A - B –> A + (-B) –> A + 2's compl (B)
23
Some subtractions A 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1 2 4
(-1) - (-1) (-1)
24
Some subtractions B 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1
(-1) 2 C - (-1) (-1) 0 C -4 C C
25
Some subtractions C 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1
(-1) 2 C OK BAD - (-1) (-1) 0 C OK OK -4 C C OK BAD
26
Some subtractions D 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1
(-1) 2 C OK BAD - (-1) (-1) 0 C OK OK -4 C C OK BAD
27
Error during signed subtraction:
R = A - B A, B different sign and B, R same sign called overflow
28
Arithmetic- logic unit (ALU)
Condition codes C, V, N, Z 32 A 32 C 32 B Operation C = carry V = overflow N = sign bit of R Z = 1 if R = 0
29
Compare two unsigned numbers?
is A < B ? Easy! Compute A - B and examine carry But – to compare two signed numbers? Most common mistake: Compute R = A - B, then look at sign of R. If R < 0 then A < B (N-bit) Not good enough!
30
To compare two signed numbers:
What about A = - 4 B = 3 (- 4) c 001 Assumption: “If R neg then A < B” We conclude A B, that is - 4 3 Wrong!
31
Some examples A 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1
(-1) - (-1) (-1)
32
Some examples B 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1
(-1) 2 C 3 < +1? No! < -1? No! - (-1) (-1) 0 C -1 < -1? No! < -1? No! -4 C C -3 < +1? Yes! < 1? Yes!
33
Some examples C 3 0 1 1 3 0 1 1 - 1 + 1 1 1 -(-1) + 0 0 1
(-1) 2 C 3 < +1? No! < -1? No! N = 0, V = N = 1, V = 1 - (-1) (-1) 0 C -1 < -1? No! < -1? No! N = 0, V = N = 0, V = 0 -4 C C -3 < +1? Yes! < 1? Yes! N = 1, V = N = 0, V = 1
34
To compare signed numbers:
Compute R = A - B A < B true if N and V are different A<B = exor(N,V) after computation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.