Professional Documents
Culture Documents
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 1 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 2
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 3 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 4
8/16/2018
We Can Choose Any Meaning for a Bit Pattern That’s One Way to Define 2’s Complement
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 5 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 6
2’s Complement Can Also be Derived Algebraically Properties Needed for Negative Number Bit Patterns
We can also define N-bit 2’s complement For each number K, 0 < K < 2N-1,
algebraically. ◦ we want to find an N-bit pattern PK,
An adder for N-bit unsigned gives 0 ≤ PK < 2N,
SUMN(A,B) = A + B mod 2N ◦ such that for any integer M,
N-bit 2’s complement includes positive (-K + M = PK + M) mod 2N
numbers in the range [1, 2N-1 – 1]. These bit
patterns all start with a “0” bit. The bit pattern PK then produces the same
results as -K when used with unsigned
We need to find bit patterns for negative arithmetic.
numbers.
Also, PK must not be used by a number ≥ 0.
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 7 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 8
8/16/2018
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 9 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 10
Negating Twice Gives an Identity Operation Is There an Easy Way to Find -K?
We know that -K is 2N – K.
100000 (2N)
Substituting once, we obtain - (2N – K). - ????? (K)
Substituting again, we obtain 2N – (2N – K).
But that seems painful.
But that’s just K, as we expect.
Instead, notice that 2N = (2N – 1) + 1.
What name? Oh, “2’s complement?”
So we can calculate (2N – 1) – K + 1.
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 11 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 12
8/16/2018
2’s Complement is 1’s Complement Plus One! Distinguish 2’s Complement from Negation
Again for N=5: Here or elsewhere, you will hear the phrase
11111 (2N – 1) “take the 2’s complement.”
- ????? (K) We will try not to use “2’s complement” in
that way.
(answer)
+ 1 Students get confused between the
2’s complement representation for signed
The first step is trivial: replace 0 with 1, and 1 integers and the operation of negation on a
with 0. The result ((2N – 1) – K) is called the bit pattern for a number represented with
1’s complement of K. 2’s complement.
Adding 1 more gives the 2’s complement. For clarity, we suggest that you do the same.
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 13 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 14
Example: Negating a Number in 2’s Complement 2’s Complement Conversion Can Be Same as Unsigned
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 15 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 16
8/16/2018
Use Two Negations to Convert Negative Numbers Alternate Method for Calculating 2’s Complement Values
To convert decimal D < 0 to 2’s complement, If we have a negative number -K, we can use
◦ first convert –D (as unsigned), the base 2 polynomial to calculate 2N – K:
◦ then negate the resulting bit pattern.
2N – K = aN-12N-1 + aN-22N-2 + … + a020
To convert a negative 2’s complement bit We know that aN-1 = 1 for a negative number.
pattern (a bit pattern starting with 1) to Substituting and subtracting 2N gives:
decimal,
◦ first negate the bit pattern, -K = (2N-1 – 2N) + aN-22N-2 + … + a020
◦ then convert to decimal D (as unsigned). -K = -aN-12N-1 + aN-22N-2 + … + a020
◦ The answer is -D. This polynomial also works when aN-1 = 0.
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 17 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 18
What about the Last Bit Pattern? Extend Unsigned Bit Patterns by …
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 19 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 20
8/16/2018
ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 21 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 22