How to build a 1-bit magnitude comparator from logic gates
A 1-bit magnitude comparator compares two bits A and B and raises exactly one of three outputs: A > B = A AND NOT B, A < B = NOT A AND B, and A = B = A XNOR B. It is the building block that every wider comparator repeats once per bit.
With one bit each, A is greater only in the row A = 1, B = 0, and smaller only in the row A = 0, B = 1. The equal output is 1 in the other two rows, so it is the XNOR of the inputs; this circuit gets it as NOR(GT, LT), since two bits are equal exactly when neither is larger. A 2-bit comparator uses these three signals for each bit, letting the high bit decide and the low bit break a tie, and the 4-bit magnitude comparator chains the same idea down four bits. All four rows are checked before the page is built, and the live circuit starts at A = B = 0, so EQ is lit.
Open this circuit in the lab →
What you need
- 2× NOT: NOT A and NOT B
- 2× AND: GT = A AND NOT B, LT = NOT A AND B
- 1× NOR: EQ = NOT (GT OR LT), the same as A XNOR B
Step by step
- Place inputs
AandBand three probesGT,EQandLT. - Invert both inputs with two NOT gates.
- GT: an AND of A and NOT B. LT: an AND of NOT A and B.
- EQ: a NOR of GT and LT, so it is 1 exactly when neither is 1.
- Try all four rows: exactly one output is lit in each.
1-bit comparator truth table
| A | B | GT (A > B) | EQ (A = B) | LT (A < B) |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 0 |
Exactly one output is 1 in every row. As equations: GT = A B', LT = A' B and EQ = A' B' + A B.
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 1-bit magnitude comparator in the lab →Frequently asked
What is a 1-bit magnitude comparator?
A combinational circuit that compares two single bits A and B and says whether A > B, A = B or A < B. Its equations are A > B = A B', A < B = A' B and A = B = A XNOR B.
How many gates does a 1-bit comparator need?
Five in this version: two NOT, two AND and one NOR. With an XNOR gate available, EQ can come straight from A XNOR B instead of the NOR.
How do you build a 2-bit comparator from 1-bit comparators?
Compare the high bits first. A > B when A1 > B1, or when A1 = B1 and A0 > B0; A = B when both bit pairs are equal; A < B is the mirror image. The same pattern extends to any width.
What is the difference between a comparator and an XOR gate?
An XOR or XNOR gate only says whether two bits are equal. A magnitude comparator also says which one is larger.