You are on page 1of 6

8/16/2018

Strategy: Use Common Hardware for Two Representations


University of Illinois at Urbana-Champaign
Dept. of Electrical and Computer Engineering Recall:
◦ addition of bit patterns in
N-bit unsigned representations
ECE 120: Introduction to Computing ◦ corresponds to arithmetic mod 2N.
Using this arithmetic, we develop the
2’s complement representation
for signed integers.
Signed Integers and 2’s Complement The same hardware can then perform
arithmetic for both representations.
What about the name? Later.

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

Graphical Illustration of Modular Arithmetic Representations Must be Unambiguous

The circle illustrates 0 The same circle illustrates 0, 8


3-bit unsigned. 7 1 equality mod 8. -1, 7 1, 9
000 000
Adding a number 111 001 For example, we can 111 001
corresponds to extend the numbers in
counting clockwise. 6 110 010 2 a clockwise direction. -2, 6 110 010 2, 10
101 011 101 011
The answer is +3 100 Or the other way. 100
always correct 5 3 Overflow occurs because -3, 5 3, 11
mod 8. a representation can have
4 -4, 4, 12
(For subtraction, count the other way.) only one value per bit pattern.

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

But what if we pick a +3 0 Draw a circle for N bits (2N points).


different set of labels? -1 1 Starting at 0 at the top.
000
The arithmetic 111 001 Write unsigned bit patterns clockwise
doesn’t change. around the circle.
-2 110 010 2
Let’s include both Starting again from 0,
101 011
positive and negative
numbers!
100 ◦ find bit patterns for negative numbers
-3 3
◦ by moving counter-clockwise.
And try some addition.
What about the name? Later.

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

Do Algebra to Define Negative Patterns Final Answer: -K is Represented by 2N – K


Starting with our property, One easy solution to (2N – K = PK) mod 2N is
PK = 2N – K.
(-K + M = PK + M) mod 2N,
subtract M from both sides to obtain Since 0 < K < 2N-1, this solution
gives 2N-1 < PK < 2N.
(-K = PK) mod 2N. But these are all unused bit patterns—the
Next, note that patterns starting with “1!”
(2N = 0) mod 2N. So we’re done:
-K is represented by the pattern 2N – K.
Now add the last two equations to obtain
What about the name? Are you really ready?
(2N – K = PK) mod 2N.

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?

Let’s do a sanity check. How do we calculate 2N – K?


What is the bit pattern for - (-K)? We can subtract (for example, with N=5)…

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

Let’s do an example of negation with For non-negative numbers (bit patterns


8-bit 2’s complement. starting with 0),
As you know, I like 42. conversion between decimal value and 2’s
complement bit pattern
As you may remember, 4210 = 00101010.
is identical to conversion for the
So what’s -42? unsigned representation.
First, complement the bits: 11010101.
Then add 1: 11010110 = -4210!

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 …

We didn’t define a value 0 In some cases, we need


for one of the bit patterns! -1 1 ◦ to convert a bit pattern
000 ◦ from a smaller representation (fewer bits)
What should it be? 111 001
◦ to a larger one (more bits)
2N-1? -2N-1? Undefined? -2 110 010 2
How do we convert N-bit unsigned to
In 2’s complement, the 101 011 (N+k)-bit unsigned (for k > 0)?
pattern always means -2N-1. 100
-3 3 Hint: We already had to solve a similar problem
Why? So that any pattern when a number does not require N bits in base 2.
starting with a 1 is negative! -4, 4
Add k more leading 0s (called zero extension).

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

What about 2’s Complement? Extend 2’s Complement Bit Patterns by …

How do we convert N-bit 2’s complement to In 5-bit 2’s complement,


(N+k)-bit 2’s complement (for k > 0)? -510 has bit pattern 11011
-1010 has bit pattern 10110 (spaces added to
For non-negative values, help us humans)
And in 8-bit 2’s complement?
◦ 2’s complement is the same as unsigned -510 has bit pattern 111 11011
(with an extra 0 for the sign) -1010 has bit pattern 111 10110
◦ So add k more leading 0s. So how do we convert N-bit 2’s complement to
(N+k)-bit 2’s complement (for k > 0)?
What about negative values? Add k copies of the sign bit
(called sign extension).

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

You might also like