# 程序代写代做代考 flex Microsoft PowerPoint – 053-2’s-complement-comparator

Microsoft PowerPoint – 053-2’s-complement-comparator

9/25/2016

University of Illinois at Urbana-Champaign
Dept. of Electrical and Computer Engineering

ECE 120: Introduction to Computing

A Comparator for 2’s Complement

Comparing 2’s Complement Is Different from Unsigned

Let’s design a comparator for
2’s complement numbers.
Is the function the same as
For unsigned, 1001 > 0101.

Is the same true with 2’s complement?
No.

Should we just start over?

Let’s try a little harder first…
If we compare two non-negative numbers,
◦ the approach IS the same.
◦Right?

Maybe we can just use some extra logic to
handle the sign bits?

Consider All Possible Combinations of Sign Bits

Let’s make a table based on the sign bits:

As Bs interpretation solution
0 0 A ≥ 0 AND B ≥ 0 use unsigned

comparator
0 1 A ≥ 0 AND B < 0 1 0 A < 0 AND B ≥ 0 1 1 A < 0 AND B < 0 A > B
A < B unknown 9/25/2016 Interpret 2’s Complement as Unsigned Remember our “simple” rule for translating 2’s complement bit patterns to decimal? The pattern A = aN-1aN-2 … a1a0 has value VA = -aN-12N-1 + aN-22N-2 + … + a020 Let A be negative (aN-1 = 1). Interpreted as unsigned, the same bits have value VA + 2N.* *The statement is true by definition of 2’s complement, actually. ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 5 Negative Numbers Can be Compared Directly What happens if we feed two negative 2’s complement numbers into our unsigned comparator? We compare VA + 2N with VB + 2N. And we get an answer: <, =, or >.
Let’s say that we find VA + 2N < VB + 2N. In that case, VA < VB, so we have the right answer for 2’s complement. The same result holds for other answers. ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 6 We Need Special Logic for the Sign Bits Now we can complete our table: ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 7 As Bs interpretation solution 0 0 A ≥ 0 AND B ≥ 0 use unsigned comparator 0 1 A ≥ 0 AND B < 0 A > B
1 0 A < 0 AND B ≥ 0 A < B 1 1 A < 0 AND B < 0 use unsigned comparator Simply Flip the Wires on the Most Significant Bit Can we just flip the wires on the sign bits? For As = 0 and Bs = 1, ◦ we feed in AN-1 = 1 and BN-1 = 0, and ◦ the unsigned comparator produces A > B.

For As = 1 and Bs = 0,
◦ we feed in AN-1 = 0 and BN-1 = 1, and