Is A greater, equal, or less than B? Exactly one lamp will tell you. A ready-made arithmetic circuit you can open in the TorchAnvil simulator.
Given two bits, there are only three possible relationships between them: A is bigger, A equals B, or A is smaller. A one-bit comparator answers all three at once, lighting exactly one of three lamps for any input.
Each lamp has its own tiny formula:
A AND NOT B. The only way A beats B in one bit is
A = 1 and B = 0.A XNOR B. XNOR is "both the same" — true when both are
off or both are on.NOT A AND B. The mirror of the greater-than case.Because the three conditions are mutually exclusive and cover every input, exactly one lamp is lit at any moment. Always. No matter what you do with the levers.
| A | B | A>B |
A=B |
A<B |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 0 |
This is the building block for any magnitude comparator. To compare
multi-bit numbers, you chain these from the most significant bit down:
if the top bits differ, that's the answer; if they're equal, fall
through to the next bit. The same "is A greater, equal, or less"
cascade shows up in sorting networks, priority encoders, and every
if (a > b) your CPU has ever evaluated.