Digital Comparators
A digital comparator determines whether one binary number is equal to, greater than, or less than another, bit by bit.
Comparing Numbers in Logic
A digital comparator takes two binary numbers and reports their relationship: equal, greater than, or less than. It is a fundamental combinational building block, used in sorting, address decoding, control flow (evaluating conditions), and threshold detection. A comparator producing all three outcomes is called a magnitude comparator.
Equality First
Equality is the simplest case. Two bits are equal when their XNOR is 1 (they match); two numbers are equal when every bit position matches, so the equality output is the AND of the per-bit XNORs. A single-bit comparator's equality term, a_i XNOR b_i, is the elementary piece from which wider comparisons are built.
| 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 |
Magnitude Comparison
To decide which number is larger, a comparator scans from the most significant bit down. At the first position where the bits differ, the number with a 1 there is larger and the decision is made; positions below no longer matter. This mirrors how a priority encoder finds the most significant difference. Formally, a is greater than b if there exists a highest position i where a_i is 1, b_i is 0, and all higher bits are equal.
Building Wide Comparators
A ripple comparator chains single-bit stages, passing an 'equal so far' signal down and letting each stage override the result when it finds a difference; its delay grows with width, much like a ripple-carry adder. Faster designs compute the comparison in a tree of logarithmic depth, and subtraction-based comparators reuse an adder: computing a minus b and inspecting the sign and zero flags yields all three relations. Comparators appear inside CAM match logic, address decoders, and the condition-evaluation hardware of every processor.