8-bit magnitude comparator: GT, EQ and LT, live
An 8-bit magnitude comparator compares two bytes, A7..A0 and B7..B0, and raises exactly one of three outputs: GT when A is greater, EQ when they are equal, or LT when A is less. The answer is decided at the highest bit where A and B differ.
Comparing binary numbers works like comparing decimal ones: start at the most significant digit and stop at the first difference. For each bit the circuit computes e_i = A_i XNOR B_i (the bits are equal) and g_i = A_i AND NOT B_i (A has the 1). A is greater if bit 7 says so, or bits 7 are equal and bit 6 says so, and so on down: GT = g7 + e7 g6 + e7 e6 g5 + ... + e7 e6 e5 e4 e3 e2 e1 g0. A running AND of the e signals gives every "all higher bits equal" condition, and its last value is EQ. Exactly one output is 1, so LT is simply NOR(GT, EQ). The 4-bit magnitude comparator is the same circuit half as wide, and the 7485 chip does 4 bits with cascade inputs so two of them make an 8-bit comparator.
Open this circuit in the lab →
What you need
- 8× XNOR: e_i: bit i of A equals bit i of B
- 8× NOT: NOT B_i
- 8× AND: g_i = A_i AND NOT B_i
- 14× AND: the running "all higher bits equal" products and each GT term
- 7× OR: summing the GT terms
- 1× NOR: LT = NOR(GT, EQ)
Step by step
- Place inputs
A7toA0andB7toB0, and probesGT,EQandLT. - For each bit, make e_i = A_i XNOR B_i and g_i = A_i AND NOT B_i.
- Chain ANDs of e7, e6, ... to get "all higher bits equal" for each position; AND each with that position's g_i and OR the results, starting with g7, to get GT. The last AND of the chain is EQ.
- LT = NOR(GT, EQ). Set two bytes and flip bits from the top down: the first difference decides.
Comparator examples
| A | B | Highest differing bit | GT | EQ | LT |
|---|---|---|---|---|---|
| 1001 0110 (150) | 1001 0110 (150) | none | 0 | 1 | 0 |
| 1000 0000 (128) | 0111 1111 (127) | 7: A has the 1 | 1 | 0 | 0 |
| 0110 0101 (101) | 0110 1000 (104) | 3: B has the 1 | 0 | 0 | 1 |
| 1111 1110 (254) | 1111 1111 (255) | 0: B has the 1 | 0 | 0 | 1 |
The full table has 2^16 rows, so before the page is built the circuit is checked on 1,377 of them: every value against itself, every one-bit difference in each direction over a spread of values, every pair of neighbours (A = B plus or minus 1) and 1,000 seeded random pairs.
You can also read the full theory in the lesson on comparators. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
More builds: Comparing numbers
All 246 build guides, by topic
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the 8-bit magnitude comparator in the lab →Frequently asked
How does an 8 bit magnitude comparator work?
It compares the two numbers from the most significant bit down. The first bit where they differ decides: if A has the 1 there, A is greater; if B does, A is less; if no bit differs, they are equal.
How do you build an 8 bit comparator from 4 bit comparators?
Compare the high halves first and use the low halves only when the high halves are equal: GT = GT_high + EQ_high GT_low, EQ = EQ_high EQ_low, LT = LT_high + EQ_high LT_low. The 7485 chip has cascade inputs for exactly this.
What are the outputs of a magnitude comparator?
Three: A greater than B, A equal to B, and A less than B. Exactly one of them is 1 for any pair of inputs.