# Comparators Explained

concept

The idea of whether one binary number is equal to another, or which of two binary numbers is the larger, is a fundamental operation you are likely to want to be able to perform.
A comparator is a device (or a circuit you build yourself), which determines which of two binary numbers is the larger or tells you if they're equal.

If you were to compare to binary numbers yourself you would obviously read them both left-to-right and compare each bit.
The first one to have a "1" when the other has a "0" is clearly the larger of the two numbers (assuming we're only using positive numbers) and if we make it to the end without deciding which is larger they must be the same.
It makes sense then that the first part in designing a comparator is to find some way to check whether two bits are equal or not.
It turns out that a simple gate called the Exclusive NOR gate (XNOR) can achieve this. The XNOR gate is an XOR gate followed by an inverter.

fact

An XNOR gate's symbol is:

fact

The truth table for the XNOR gate is:

As you can see it outputs a "1" whenever both inputs are equal and a "0" otherwise.

A | B | Output |
---|---|---|

0 | 0 | 1 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

So to build a circuit that tells is whether two binary numbers are equal or not we just need to check if each of the bits are equal.
We accomplish this by using an XNOR gate on each of the corresponding bits of our two numbers and AND-ing the results.
If all of the pairs of bits are equal our AND will output a "1" and our numbers are equal.
If any two of the bits aren't the same their XNOR gate will output a "0" and the total output is "0".

fact

A simple comparator can be built by using XNOR gates to compare the corresponding bits on two binary numbers and using an AND gate to check that all outputs are "1".

So that's pretty good but if the two numbers aren't equal we have no way of checking which one is larger, all we know is that they're different.
We can buy a chip called a "comparator" that will do this comparison for us. You supply it two binary numbers and it has three outputs. One tells you if they're equal, another tells you if the first number is larger than the second and the last tells you if the second number is larger than the first.

fact

A comparator is a chip with several inputs and three outputs.
You apply your two binary numbers A and B to the inputs.
The first output is high if \(A\gt B\)
The second output is high if \(A = B\)
The third output is high if \(A\lt B\)

example

Determine the output for the comparator below:

We have \(A = 1011\) and \(B = 1101\) So since \(A\gt B\) the top output will be a "1" while the other two outputs will be "0". Now say you have some chips that compare 4-bit numbers but you want to get fancy and compare some 8-bit numbers.
Well luckily there's a clever way to hook up multiple comparators to achieve this.

fact

You can extend two 4-bit comparators to compare 8-bit digits by arranging them like so:
Comparator chips have special carry inputs just for this purpose.

practice problems