Skip to content

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?"

ABXORXNOR
0001
0110
1010
1101

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:

EQ = XNOR(A1, B1) AND XNOR(A0, 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.

Try it yourself: free lesson

“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:

ZERO = NOT (A1 OR A0)

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.

Try it yourself: free lesson

“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 needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3A = 10 and B = 11. EQ is…
    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.
  2. Question 2 of 3Of the 16 ways to set two 2-bit numbers, how many make EQ 1?
    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.
  3. Question 3 of 3Use XOR instead of XNOR for one bit. For A = 00, B = 00 that XOR gives…
    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.
Keep practising: free lesson

“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.

Every topic