Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at...
-
Upload
mollie-kennard -
Category
Documents
-
view
215 -
download
0
Transcript of Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at...
![Page 1: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/1.jpg)
Datorteknik IntegerAddSub bild 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 000
One + 001
001
Zero + one = one. Makes sense!
![Page 2: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/2.jpg)
Datorteknik IntegerAddSub bild 2
Add one repeatedly, use up all possible patterns:
Zero 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
Called the
Unsigned Integer System
No negative integers!
![Page 3: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/3.jpg)
Datorteknik IntegerAddSub bild 3
Two additions:
2 010
+ 3 + 011
5
4 100
+ 5 + 101
9
![Page 4: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/4.jpg)
Datorteknik IntegerAddSub bild 4
Two additions:
2 010
+ 3 + 011
5 101
Yes! 5 = 101
4 100
+ 5 + 101
9 001
9 001; 001 represents one.
is 4 + 5 = 1???
![Page 5: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/5.jpg)
Datorteknik IntegerAddSub bild 5
Addition of unsigned integers
Error detected by presence of
"carry"
![Page 6: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/6.jpg)
Datorteknik IntegerAddSub bild 6
How do we subtract unsigned integers?
We need the concept of the
"Two's complement"
![Page 7: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/7.jpg)
Datorteknik IntegerAddSub bild 7
One's complement
Take any string
Invert every bit
0 1
This is One's complement "NOT"
![Page 8: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/8.jpg)
Datorteknik IntegerAddSub bild 8
Two's complement
Given a string
One's complement
then add one
This is called
two's complement
![Page 9: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/9.jpg)
Datorteknik IntegerAddSub bild 9
To subtract unsigned A-B
Perform
A + 2's comp (B)
= A + Not (B) + 1
![Page 10: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/10.jpg)
Datorteknik IntegerAddSub bild 10
Example:
5 101 101
- 3 - 011 + 100
2 + 1
010
3 011 011
- 5 - 101 + 010
- 2 + 1
110
Carry!
=2; Good!
No Carry!
=6; BAD!
![Page 11: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/11.jpg)
Datorteknik IntegerAddSub bild 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
![Page 12: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/12.jpg)
Datorteknik IntegerAddSub bild 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!
![Page 13: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/13.jpg)
Datorteknik IntegerAddSub bild 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
![Page 14: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/14.jpg)
Datorteknik IntegerAddSub bild 14
Repeatedly add -1:
Zero 000
- 1 111
- 2 110 Less than
- 3 101 zero
- 4 110
- 5 011 No!
High order bit called "sign bit"
![Page 15: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/15.jpg)
Datorteknik IntegerAddSub bild 15
Signed 3-bit integers
3 011
2 010
1 001
0 000
-1 111
-2 110
-3 101
-4 100Not symmetrical around zero!!!
![Page 16: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/16.jpg)
Datorteknik IntegerAddSub bild 16
Sign bit
The high order bit in a number
Also called "N"-bit
Value is negative when
this bit is "1"
![Page 17: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/17.jpg)
Datorteknik IntegerAddSub bild 17
Let's try A + B
1 001 1 001
+2 010 +(-1) 111
3 011 0 *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
![Page 18: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/18.jpg)
Datorteknik IntegerAddSub bild 18
Some additions A
1 0 0 1 2 0 1 0
+2 0 1 0 + 2 0 1 0
3 4
-1 1 1 1 -1 1 1 1
+(-3) 1 0 1 +(-4) 1 0 0
-4 -5
1 0 0 1 -2 1 1 0
+(-2) 1 1 0 +1 0 0 1
-1 -1
![Page 19: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/19.jpg)
Datorteknik IntegerAddSub bild 19
Some additions B
1 0 0 1 2 0 1 0
+2 0 1 0 + 2 0 1 0
3 0 1 1 4 1 0 0
-1 1 1 1 -1 1 1 1
+(-3) 1 0 1 +(-4) 1 0 0
-4 C 1 0 0 -5 C 0 1 1
1 0 0 1 -2 1 1 0
+(-2) 1 1 0 +1 0 0 1
-1 1 1 1 -1 1 1 1
![Page 20: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/20.jpg)
Datorteknik IntegerAddSub bild 20
Some additions C
1 0 0 1 2 0 1 0
+2 0 1 0 + 2 0 1 0
3 0 1 1 4 1 0 0
3 -4
OK BAD
-1 1 1 1 -1 1 1 1
+(-3) 1 0 1 +(-4) 1 0 0
-4 C 1 0 0 -5 C 0 1 1
-4 3
OK BAD
1 0 0 1 -2 1 1 0
+(-2) 1 1 0 +1 0 0 1
-1 1 1 1 -1 1 1 1
-1 -1
OK OK
![Page 21: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/21.jpg)
Datorteknik IntegerAddSub bild 21
Some additions D
1 0 0 1 2 0 1 0
+2 0 1 0 + 2 0 1 0
3 0 1 1 4 1 0 0
3 -4
OK BAD
-1 1 1 1 -1 1 1 1
+(-3) 1 0 1 +(-4) 1 0 0
-4 C 1 0 0 -5 C 0 1 1
-4 3
OK BAD
1 0 0 1 -2 1 1 0
+(-2) 1 1 0 +1 0 0 1
-1 1 1 1 -1 1 1 1
-1 -1
OK OK
![Page 22: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/22.jpg)
Datorteknik IntegerAddSub bild 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)
![Page 23: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/23.jpg)
Datorteknik IntegerAddSub bild 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 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
0 2
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
-4 - 5
![Page 24: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/24.jpg)
Datorteknik IntegerAddSub bild 24
Some subtractions B
3 0 1 1 3 0 1 1
- 1 + 1 1 1 -(-1) + 0 0 1
2 C 0 1 0 4 1 0 0
-1 1 1 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
0 C 0 0 0 2 0 1 0
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
-4 C 1 0 0 - 5 C 0 1 1
![Page 25: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/25.jpg)
Datorteknik IntegerAddSub bild 25
Some subtractions C
3 0 1 1 3 0 1 1
- 1 + 1 1 1 -(-1) + 0 0 1
2 C 0 1 0 4 1 0 0
2 -4
OK BAD
-1 1 1 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
0 C 0 0 0 2 0 1 0
0 2
OK OK
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
-4 C 1 0 0 - 5 C 0 1 1
-4 3
OK BAD
![Page 26: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/26.jpg)
Datorteknik IntegerAddSub bild 26
Some subtractions D
3 0 1 1 3 0 1 1
- 1 + 1 1 1 -(-1) + 0 0 1
2 C 0 1 0 4 1 0 0
2 -4
OK BAD
-1 1 1 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
0 C 0 0 0 2 0 1 0
0 2
OK OK
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
-4 C 1 0 0 - 5 C 0 1 1
-4 3
OK BAD
![Page 27: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/27.jpg)
Datorteknik IntegerAddSub bild 27
Error during signed subtraction:
R = A - B
A, B different sign
and
B, R same sign
called overflow
![Page 28: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/28.jpg)
Datorteknik IntegerAddSub bild 28
Arithmetic- logic unit (ALU)
C = carry
V = overflow
N = sign bit of R
Z = 1 if R = 0
32
32
32
A
B
C
Operation
Condition codesC, V, N, Z
![Page 29: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/29.jpg)
Datorteknik IntegerAddSub bild 29
Compare two unsigned numbers?
is A < B ?
Easy! Compute A - B and examine carry
But – to compare two signed numbers?
is A < B ?
Most common mistake:– Compute R = A - B, then look at sign
of R.
– If R < 0 then A < B (N-bit)
Not good enough!
![Page 30: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/30.jpg)
Datorteknik IntegerAddSub bild 30
To compare two signed numbers:
What about
A = - 4B = 3
(- 4) 100 - 3 + 101 c 001
Assumption:“If R neg then A < B”
We conclude A B, that is - 4 3
Wrong!
![Page 31: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/31.jpg)
Datorteknik IntegerAddSub bild 31
Some examples A
3 0 1 1 3 0 1 1
- 1 + 1 1 1 -(-1) + 0 0 1
-1 1 1 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
![Page 32: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/32.jpg)
Datorteknik IntegerAddSub bild 32
Some examples B
3 0 1 1 3 0 1 1
- 1 + 1 1 1 -(-1) + 0 0 1
2 C 0 1 0 4 1 0 0
3 < +1? No! 3 < -1? No!
-1 1 1 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
0 C 0 0 0 2 0 1 0
-1 < -1? No! 1 < -1? No!
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
-4 C 1 0 0 - 5 C 0 1 1
-3 < +1? Yes! -4 < 1? Yes!
![Page 33: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/33.jpg)
Datorteknik IntegerAddSub bild 33
Some examples C
3 0 1 1 3 0 1 1
- 1 + 1 1 1 -(-1) + 0 0 1
2 C 0 1 0 4 1 0 0
3 < +1? No! 3 < -1? No!
N = 0, V = 0 N = 1, V = 1
-1 1 1 1 1 0 0 1
- (-1) + 0 0 1 - (-1) + 0 0 1
0 C 0 0 0 2 0 1 0
-1 < -1? No! 1 < -1? No!
N = 0, V = 0 N = 0, V = 0
- 3 1 0 1 - 4 1 0 0
- 1 + 1 1 1 - 1 + 1 1 1
-4 C 1 0 0 - 5 C 0 1 1
-3 < +1? Yes! -4 < 1? Yes!
N = 1, V = 0 N = 0, V = 1
![Page 34: Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.](https://reader035.fdocuments.us/reader035/viewer/2022062621/551c06e55503469e4f8b4f01/html5/thumbnails/34.jpg)
Datorteknik IntegerAddSub bild 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