Computing Library › Digital Logic & Circuits
Digital Logic & Circuits

Digital Comparator

A digital comparator tests two binary numbers and reports whether one is greater than, equal to, or less than the other.

What it does

A comparator takes two n-bit numbers A and B and produces up to three outputs: A greater than B, A equal to B, and A less than B. Exactly one is true for any pair of inputs.

One-bit equality

Two single bits are equal when they match, given by XNOR: E = NOT(A XOR B). Full equality of two words is the AND of the per-bit equality signals across all positions.

Magnitude comparison

To decide which is larger, the comparator scans from the most significant bit down. At the highest position where the bits differ, the number with a 1 there is greater. Formally, A greater than B is true if, reading from the top, the first differing bit has A = 1 and B = 0.

Cascading

Comparators include inputs to chain multiple units, so a wide comparison is built from narrower blocks. Lower-order blocks pass their result up, and higher-order blocks take precedence when their bits differ.

Uses

Comparators drive conditional branches, sort and search hardware, and threshold detection. A subtractor can also compare by examining the sign and zero flags of A - B, which is how comparison is usually done inside an ALU.