EN

Magnitude comparators

Compare two binary numbers and see which bit decides, with one or two cascaded 74HC85

Simulator

Comparator

Bit-by-bit comparison

What a magnitude comparator does

A magnitude comparator receives two binary numbers A and B and activates one of three outputs: A > B, A = B or A < B. It is used to know whether a count has reached a value, whether a measurement exceeds a threshold, to sort data or to decide a jump in a processor. It is a combinational circuit: the output depends only on the inputs at that moment.

One-bit comparator

With a single bit per number there are four cases, and each output is a simple gate:

\[ (A>B) = A\,\overline{B} \qquad (A<B) = \overline{A}\,B \]
\[ (A=B) = \overline{A \oplus B} = A \odot B \]

Equality is an XNOR: it is 1 when both bits match.

4-bit comparator

For multi-bit numbers the comparison is done as with decimal numbers: starting with the most significant digit. If A3 and B3 differ, that bit decides; if they are equal, move to the next one, and so on to the last. Calling xi = Ai ⊙ Bi the equality of each bit:

\[ (A=B) = x_3\,x_2\,x_1\,x_0 \]
\[ \begin{aligned} (A>B) = {} & A_3\overline{B_3} + x_3 A_2\overline{B_2} \\ & + x_3 x_2 A_1\overline{B_1} \\ & + x_3 x_2 x_1 A_0\overline{B_0} \end{aligned} \]

A < B is obtained the same way swapping A and B, or as the output left when neither of the other two holds. In the simulator table, the yellow bit is the one that decides and the green ones are the higher bits, which match.

Cascading

Besides A and B, the 74HC85 has three cascade inputs (A>B in, A=B in, A<B in) that only count when all four bits are equal: in that case the IC copies to its outputs what arrives on them. To compare 8 bits, two are used: the low-bit one has its cascade inputs at A=B in = 1 and the others at 0, and its outputs go to the cascade inputs of the high-bit one. If the 4 high bits match, the low part decides. With more ICs in a row any number of bits can be compared, although the delay grows with each stage.

Comparing with a microcontroller or with sign

A processor has no separate comparator: it subtracts A − B with its adder and looks at the flags. Z = 1 means A = B; unsigned, C = 0 means A < B; signed, A < B when N ⊕ V = 1. The 74HC85 compares unsigned numbers; for two's complement signed numbers it is enough to invert the sign bit of A and B before comparing them.

ICFunction
74HC854-bit magnitude comparator with cascade inputs
CD40634-bit comparator from the 4000 series
74HC6888-bit equality comparator (A = B only); used for address decoding
74HC6828-bit magnitude comparator with pull-up resistors

Related: adders and subtractors, 4-bit ALU, counters (a counter plus a comparator make a timer) and Karnaugh map.