A magnitude comparator tells you how two unsigned binary numbers relate. It usually has three outputs, and exactly one of them is 1:
- G = 1 when A > B
- E = 1 when A = B
- L = 1 when A < B
One bit. G = , E = , L = .
More bits. Compare like a dictionary: look at the MSB first. If the top bits differ, they decide everything. Only if they are equal do you look at the next bit down, and so on. For 2-bit numbers A1A0 and B1B0:
G =
The first term says "A wins on the top bit". The second says "the top bits tie, and A wins on the low bit". L is the same with A and B swapped, and E comes from the equality comparator.
Two handy shortcuts:
- Since exactly one output is 1, L = .
- A ≥ B is G + E, which is also just .
Larger comparators repeat the pattern bit by bit, or are chained from 4-bit blocks that pass their results down. An ALU can also compare by subtracting and checking its status flags.
| G | E | L | ||||
|---|---|---|---|---|---|---|
| 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 |