Integer arithmetic Depends what you mean by integer
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 Zero + one = one. Makes sense! Datorteknik Integer. Add. Sub bild 1
Add one repeatedly, use up all possible patterns: Zero 1 2 3 4 5 6 7 000 001 010 011 100 101 110 111 Called the Unsigned Integer System No negative integers! Datorteknik Integer. Add. Sub bild 2
Two additions: 2 010 + 3 + 011 5 4 100 + 5 + 101 9 Datorteknik Integer. Add. Sub bild 3
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? ? ? Datorteknik Integer. Add. Sub bild 4
Addition of unsigned integers Error detected by presence of "carry" Datorteknik Integer. Add. Sub bild 5
How do we subtract unsigned integers? We need the concept of the "Two's complement" Datorteknik Integer. Add. Sub bild 6
One's complement Take any string Invert every bit 0 1 This is One's complement "NOT" Datorteknik Integer. Add. Sub bild 7
Two's complement Given a string One's complement then add one This is called two's complement Datorteknik Integer. Add. Sub bild 8
To subtract unsigned AB Perform A + 2's comp (B) = A + Not (B) + 1 Datorteknik Integer. Add. Sub bild 9
Example: 5 - 3 2 101 - 011 101 + 100 + 1 010 =2; Good! 011 + 010 + 1 110 =6; BAD! Carry! 3 - 5 - 2 011 - 101 No Carry! Datorteknik Integer. Add. Sub bild 10
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 Datorteknik Integer. Add. Sub bild 11
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! Datorteknik Integer. Add. Sub bild 12
How about negative numbers? -1? How should we represent How would we compute 0 - 1? 0 + 2's compl (1) We choose this as our "-1" 1 = 001 - 1 = 110 + 1 111 Datorteknik Integer. Add. Sub bild 13
Repeatedly add -1: Zero -1 -2 -3 -4 000 111 110 101 110 -5 011 Less than zero No! High order bit called "sign bit" Datorteknik Integer. Add. Sub bild 14
Signed 3 -bit integers 3 2 1 0 -1 -2 -3 -4 011 010 001 000 111 110 101 100 Not symmetrical around zero!!! Datorteknik Integer. Add. Sub bild 15
Sign bit The high order bit in a number Also called "N"-bit Value is negative when this bit is "1" Datorteknik Integer. Add. Sub bild 16
Let's try A + B 1 +2 3 001 010 011 1 +(-1) 0 001 111 *000 * carry Both results is OK But: Left case: no carry Right case: carry Conclusion: For signed addition carry is worthless Same conclusion for signed subtraction Datorteknik Integer. Add. Sub bild 17
Some additions A 1 +2 3 001 010 2 +2 4 010 -1 +(-3) -4 111 101 -1 +(-4) -5 111 100 1 +(-2) -1 001 100 -2 +1 -1 110 001 Datorteknik Integer. Add. Sub bild 18
Some additions B 1 +2 3 001 010 011 2 +2 4 010 100 -1 111 +(-3) 101 -4 C 1 0 0 -1 +(-4) -5 111 100 C 011 1 +(-2) -1 001 100 111 -2 +1 -1 110 001 111 Datorteknik Integer. Add. Sub bild 19
Some additions C 1 +2 3 001 010 011 3 2 +2 4 010 100 -4 BAD -1 111 +(-3) 101 -4 C 1 0 0 -4 OK -1 +(-4) -5 111 100 C 011 3 BAD OK 1 +(-2) -1 001 100 111 -1 OK -2 +1 -1 110 001 111 -1 OK Datorteknik Integer. Add. Sub bild 20
Some additions D 1 +2 3 001 010 011 3 2 +2 4 010 100 -4 BAD -1 111 +(-3) 101 -4 C 1 0 0 -4 OK -1 +(-4) -5 111 100 C 011 3 BAD OK 1 +(-2) -1 001 100 111 -1 OK -2 +1 -1 110 001 111 -1 OK Datorteknik Integer. Add. Sub bild 21
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) Datorteknik Integer. Add. Sub bild 22
Some subtractions A 3 -1 2 011 +111 3 -(-1) 4 011 +001 -1 - (-1) 0 111 +001 1 - (-1) 2 001 +001 -3 -1 -4 101 +111 -4 -1 -5 100 + 111 Datorteknik Integer. Add. Sub bild 23
Some subtractions B 3 -1 2 011 +111 C 010 3 -(-1) 4 011 +001 100 -1 111 - (-1) + 0 0 1 0 C 000 1 - (-1) 2 001 +001 010 -4 -1 -5 100 + 111 C 011 -3 -1 -4 101 +111 C 100 Datorteknik Integer. Add. Sub bild 24
Some subtractions C 3 -1 2 011 +111 C 010 2 OK 3 -(-1) 4 011 +001 100 -4 BAD -1 111 - (-1) + 0 0 1 0 C 000 0 OK 1 - (-1) 2 001 +001 010 2 OK -4 -1 -5 100 + 111 C 011 3 BAD -3 -1 -4 101 +111 C 100 -4 OK Datorteknik Integer. Add. Sub bild 25
Some subtractions D 3 -1 2 011 +111 C 010 2 OK 3 -(-1) 4 011 +001 100 -4 BAD -1 111 - (-1) + 0 0 1 0 C 000 0 OK 1 - (-1) 2 001 +001 010 2 OK -4 -1 -5 100 + 111 C 011 3 BAD -3 -1 -4 101 +111 C 100 -4 OK Datorteknik Integer. Add. Sub bild 26
Error during signed subtraction: R=A-B A, B different sign and B, R same sign called overflow Datorteknik Integer. Add. Sub bild 27
Arithmetic- logic unit (ALU) 32 Condition codes C, V, N, Z A C 32 32 B Operation C = carry V = overflow N = sign bit of R Z = 1 if R = 0 Datorteknik Integer. Add. Sub bild 28
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! Datorteknik Integer. Add. Sub bild 29
To compare two signed numbers: What about – A=-4 – B=3 – -4 – -3 – 100 + 101 c 001 “If R neg then A < B” – We conclude A ≥ B, that is -4≥ 3 Wrong! Datorteknik Integer. Add. Sub bild 30
Some examples A 3 -1 011 +111 3 -(-1) 011 +001 -1 - (-1) 111 +001 1 - (-1) 001 +001 -3 -1 101 +111 -4 -1 100 + 111 Datorteknik Integer. Add. Sub bild 31
Some examples B 3 -1 2 011 +111 C 010 3 < +1? No! -1 111 - (-1) + 0 0 1 0 C 000 -1 < -1? No! -3 -1 -4 101 +111 C 100 -3 < +1? Yes! 3 011 -(-1) +001 4 100 3 < -1? No! 1 - (-1) 2 001 +001 010 1 < -1? No! -4 -1 -5 100 + 111 C 011 -4 < 1? Yes! Datorteknik Integer. Add. Sub bild 32
Some examples C 3 -1 2 011 +111 C 010 3 < +1? No! N = 0, V = 0 -1 111 - (-1) + 0 0 1 0 C 000 -1 < -1? No! N = 0, V = 0 -3 -1 -4 101 +111 C 100 -3 < +1? Yes! N = 1, V = 0 3 011 -(-1) +001 4 100 3 < -1? No! N = 1, V = 1 1 - (-1) 2 001 +001 010 1 < -1? No! N = 0, V = 0 -4 -1 -5 100 + 111 C 011 -4 < 1? Yes! N = 0, V = 1 Datorteknik Integer. Add. Sub bild 33
To compare signed numbers: Compute R = A - B A < B true if N and V are different A<B = exor(N, V) after computation Datorteknik Integer. Add. Sub bild 34
- Slides: 34