- Published: Wednesday, 28 February 2018 15:36
- Written by Timothy Chapman
Comparators take two inputs and compare them with each other, giving one of three possible outputs. The inputs are labeled "A" and "B" while the outputs are "A > B," "A = B" and "A < B." Below are examples of a single-bit unsigned comparator (left), and a single-bit signed comparator (right). Note that due to the limitations of Logisim Evolution (which I used to make the examples below), the outputs are labeled "AGB" for "A > B," "AEB" for "A = B" and "ALB" for "A < B."
Multi-bit comparators start out with a single-bit comparator on the first stage. In the example below, the XNOR gate has been replaced with an XOR gate to save on transistors for the second stage. And all of the XOR gates are wired to a single NOR gate to produce the "A = B" output. The comparator in the diagram below is an example of an unsigned comparator. I left the bus between the first and second stage in tact to help give you an idea of what's going on. The comparator in the example below uses negative logic.
For comparing signed numbers with each other, just swap the unsigned, single-bit comparator for the highest bit with a signed, single-bit comparator. No further modifications are needed. And if you have a subtractor, then that can be used instead of an actual comparator. If the result is 0, then A and B are equal. If the result is not 0, then the underflow flag indicates whether A is less than or greater than B. If the underflow flag is 0, then A is greater than B. If the underflow flag is 1, then A is less than B.