LogicGates.org Open the simulatorSimulator

1-bit comparator

A comparator is a combinational circuit that takes two binary numbers and reports whether they are equal and, if not, which is greater.

Says whether two bits are equal, or which is bigger.

Circuit diagram

Toggle the inputs and follow the signals: green wires are high, red are low. The highlighted row of the truth table below is the one you have set.

equal 1 greater 0 less 0

Truth table

AB equalgreaterless
00 100
01 001
10 010
11 100

Boolean expressions

equal
¬(a ⊻ b) XNOR: high when they match
greater
a ∧ ¬b a is 1 and b is 0
less
¬a ∧ b b is 1 and a is 0

How it works

Comparing one bit needs three answers, and exactly one of them is true at any time. Equality is just XOR inverted, since XOR asks whether the inputs differ. Widening it to several bits means comparing from the most significant end and only looking further down when the bits so far are equal.

Where it is used

  • Testing two values for equality, which a processor does on every branch.
  • Sorting networks and min or max circuits.
  • Detecting that a counter has reached a target value.

Build it

An XOR with a NOT for equality, and two ANDs with a NOT each for the other two. Check that exactly one output is ever high.

Open the simulator

Or draw it from the expressions above with the circuit diagram generator, in either symbol standard, and export it as SVG, PNG, Verilog or VHDL.

Reference card

The diagram above as an image, black on white, for notes or a slide.

1-bit comparator logic circuit diagram with inputs a = A, b = B: equal from ¬(a ⊻ b), greater from a ∧ ¬b, less from ¬a ∧ b Click to download: 1-bit comparator circuit diagram

Questions about the 1-bit comparator

How does a 1-bit comparator work?

Three outputs. Equal is the XNOR of the two bits, 1 when they match. Greater is A ∧ ¬B, since the only way A can exceed B is A = 1, B = 0. Less is ¬A ∧ B. Exactly one of the three is high for any input.

How do you compare numbers with more than one bit?

Start at the most significant bit. If those bits differ, that decides it. If they are equal, look at the next bit down, and so on. In gates that is a chain of 1-bit comparators where each stage is only allowed to speak when every stage above it reported equal, and the equal output of the whole thing is the AND of all the per-bit equal signals.

What is a comparator used for?

Every conditional branch in a processor is a comparison; so is checking whether a counter has reached its target, sorting, and finding a minimum or maximum. A magnitude comparator chip such as the 7485 does four bits at once and cascades for wider numbers.

The other circuits