How to build a 2-bit comparator from logic gates
A 2-bit magnitude comparator compares two 2-bit numbers A = A1 A0 and B = B1 B0 and sets exactly one of three outputs: A > B = A1 B1' + (A1 XNOR B1) A0 B0', A = B = (A1 XNOR B1)(A0 XNOR B0), and A < B = A1' B1 + (A1 XNOR B1) A0' B0.
It is the textbook comparator exercise: four inputs, so a 16-row truth table and a 4-variable K-map per output. The equations above are what the K-maps simplify to, read as "the high bits decide, and if they are equal the low bits decide". The 4-bit magnitude comparator extends exactly this pattern to more bits. All 16 rows are checked against a real numeric comparison before the page is built; the live circuit starts at 00 versus 00, so EQ is lit.
Open this circuit in the lab →
What you need
- 2× XNOR: x1 = A1 XNOR B1 and x0 = A0 XNOR B0 (bit pairs equal)
- 4× NOT: A1', A0', B1', B0'
- 7× AND: A1 B1', A0 B0', A1' B1, A0' B0, the two terms gated by x1, and EQ = x1 x0
- 2× OR: collecting the greater-than terms and the less-than terms
Step by step
- Place inputs
A1,A0,B1,B0. Buildx1 = A1 XNOR B1andx0 = A0 XNOR B0. - Equal:
EQ = x1 AND x0. Both bit pairs must match. - Greater:
A1 AND NOT B1decides on the high bit alone. Otherwise, when the high bits are equal (x1),A0 AND NOT B0decides. OR the two:GT = A1B1' + x1 A0B0'. - Less: the mirror image,
LT = A1'B1 + x1 A0'B0. - Probe
GT,EQandLTand step through a few rows of the table below.
2-bit comparator truth table
| A1 | A0 | B1 | B0 | GT | EQ | LT |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 | 0 | 1 | 0 |
Sixteen rows: GT is 1 in six of them, LT in six, and EQ in the four on the diagonal (A = B). From a K-map the two-level forms are GT = A1B1' + A0B1'B0' + A1A0B0' and LT = A1'B1 + A1'A0'B0 + A0'B1B0; the XNOR form used in the circuit is the same function, written to show the "high bits first" idea and to share x1 between GT and LT.
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.
Build more parts from gates
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 2-bit comparator in the lab →Frequently asked
What are the equations of a 2-bit comparator?
A > B = A1B1' + A0B1'B0' + A1A0B0', A < B = A1'B1 + A1'A0'B0 + A0'B1B0, and A = B = (A1 XNOR B1)(A0 XNOR B0). Grouped by bit, A > B = A1B1' + (A1 XNOR B1)A0B0'.
How many rows does a 2-bit comparator truth table have?
Sixteen, because there are four inputs (A1, A0, B1, B0) and 2^4 = 16. Exactly one output is 1 in every row.
How is a 2-bit comparator different from a 1-bit comparator?
A 1-bit comparator only looks at one pair: A > B = AB', A < B = A'B, A = B = A XNOR B. The 2-bit version uses the high-bit comparison first and only falls back to the low bits when the high bits are equal.
How do you design a 2-bit comparator with a K-map?
Write the 16-row truth table, draw a 4-variable K-map for each output, and group the 1s. GT gives three groups (A1B1', A0B1'B0', A1A0B0'), LT gives the mirror three, and EQ has four isolated 1s that factor into (A1 XNOR B1)(A0 XNOR B0).