Computer Architecture
How a digital equality comparator works
An equality comparator is the circuit that answers "are these two binary numbers the same?" It uses one XNOR gate per bit, which gives 1 when that pair of bits matches, and an AND of all the matches. The output, EQ, is 1 only if every bit matches. One wrong bit anywhere and it's 0.
XNOR asks "the same?"
| A | B | XOR | XNOR |
|---|---|---|---|
| 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
XOR is 1 when its inputs differ; XNOR, XOR with a NOT on the output, is 1 when they're the same. That's a one-bit equality test.
AND the matches
For two 2-bit numbers, A = A1 A0 and B = B1 B0:
Take A = 10 and B = 11. The high bits match, so the first XNOR gives 1. The low bits are 0 and 1, so the second gives 0. AND of 1 and 0 is 0: not equal. Of the sixteen ways to set the four switches, only four light EQ: 0 with 0, 1 with 1, 2 with 2 and 3 with 3.
Live experiment
Find every equal pair
Two 2-bit numbers, A and B, on four switches. An XNOR per bit pair says whether that pair matches, and an AND of the matches is EQ. A 4 × 4 grid keeps every pair you've tried. Only four of the sixteen light EQ, and they all sit on the diagonal: equal means every bit matches.
“Branch if equal” is lesson 6 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the 5 before it first.
Wider numbers just need more of the same: 32-bit numbers take 32 XNORs and a tree of ANDs, and the answer arrives in about a nanosecond.
Is it zero?
The other test every processor needs is whether a number is zero. OR all its bits together: the result is 0 only if every bit is 0. Then invert it:
That's a NOR gate. Loops end, counts finish and searches stop on this one signal.
Branch if equal
These two tests are how programs make decisions. "Branch if equal" jumps only when EQ is 1; "branch if zero" when ZERO is. Many processors don't even need a separate comparator: they subtract B from A in the ALU, and the zero detector on the result says whether the two were equal.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Branch if equal” is lesson 6 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the 5 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: 0: the low bits differ. The high bits match (XNOR 1) but the low bits don't (XNOR 0). AND of 1 and 0 is 0.
- Why not “10”? EQ is a single yes-or-no bit, not one of the numbers.
- Why not “11”? EQ is one bit saying whether A and B match. It doesn't copy either number.
- Why not “1”? EQ needs every bit to match. The high bits do, but the low bits, 0 and 1, don't.
Show the answer
Answer: 4: each number matched with itself. EQ needs every bit to match, so A has to be B: 0 and 0, 1 and 1, 2 and 2, 3 and 3. In the grid they all sit on the diagonal.
- Why not “8”? Eight would include pairs where a bit differs. Only identical numbers light EQ.
- Why not “1”? Each of the four values, 0 to 3, matched with itself is a pair, so there are four, not one.
- Why not “16”? 16 is every possible pair. Most of them have at least one bit that differs.
Show the answer
Answer: 0, so EQ is wrongly 0 for equal numbers. XOR is 0 when its inputs match: the opposite of what EQ needs. Equal numbers read as unequal, and some unequal ones as equal.
- Why not “1”? XOR gives 1 only when its inputs differ. Two 0s match, so XOR gives 0.
- Why not “An error”? Logic gates don't raise errors. The circuit quietly gives the wrong answer.
- Why not “EQ unchanged”? Swapping XNOR for XOR flips that bit's match signal, so EQ changes.
“Branch if equal” is lesson 6 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the 5 before it first.
Questions people ask
What is the difference between XOR and XNOR?
XOR gives 1 when its two inputs differ; XNOR gives 1 when they're the same. XNOR is XOR with its output inverted, which is why it's sometimes called the equivalence gate.
How does a processor check whether two numbers are equal?
Often by subtracting one from the other in the ALU and checking the zero flag: A − B is zero only when A equals B. A branch-if-equal instruction then tests that flag. Dedicated comparators of XNORs and ANDs do the same job directly.
How do you compare which number is bigger?
With a magnitude comparator, which checks from the most significant bit down: the first bit where the numbers differ decides which is larger. Chips like the 74HC85 do it for 4 bits and can be chained for wider numbers.
Related topics
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.
- What is an ALU, and how does it work?Work out every answer at once, then let the opcode pick one: the heart of a CPU.
- Half adder vs full adderA half adder adds two bits; a full adder adds the carry too, so they chain.
- Binary numbers explainedPlaces worth 1, 2, 4 and 8: add up the bits that are on, and four wires count to 15.