Digital Comparator
A comparator compares two binary numbers and reports whether one is equal to, greater than, or less than the other.
What it does
A digital comparator takes two binary values and produces relational outputs: equal, greater than, and less than. It is a combinational function, its result depending only on the two current inputs.
One-bit comparison
| A | B | A=B | A>B | A<B |
|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 | 0 |
Gate equations for one bit
- A equals B is the XNOR of the bits: 1 when they match.
- A greater than B is A AND NOT B.
- A less than B is NOT A AND B.
- Exactly one of the three outputs is 1 for any input pair.
Comparing multi-bit words
Words are equal only when every bit pair matches, so equality is the AND of the per-bit XNOR results. Ordering is decided from the most significant bit down: the first position where the words differ determines which is larger, and lower positions matter only when all higher bits are equal.
In code
def compare(a, b):
if a == b: return 'eq'
return 'gt' if a > b else 'lt'
Where it appears
Comparators drive conditional branches, sort and search hardware, threshold detectors, and address-range checks. The equality path reuses the same XNOR-and-AND structure described for the XNOR gate, while the ordering path adds the greater-than and less-than terms.