A comparator answers questions like "is A equal to B?" and "is A bigger?" for two binary numbers. It comes in two flavors:
- An equality comparator only says equal or not. Two numbers are equal when every bit position matches, so it uses one XNOR per position (1 = this position matches) and ANDs the results.
- A magnitude comparator also reports which number is larger, usually on three outputs G (A > B), E (A = B) and L (A < B). Exactly one of them is 1.
For single bits the whole story fits in a few gates:
- G = (A is 1, B is 0)
- E = (the bits agree)
- L = (A is 0, B is 1)
For multi-bit numbers, compare the way you compare words in a dictionary: start at the MSB. The first position where the numbers differ decides which is larger. Lower bits only matter while every higher bit is equal.
Where it shows up: branch instructions ("jump if A < B"), sorting hardware, thermostats and timers that compare a count with a target. A CPU often does comparisons by subtracting and checking the status flags instead.