XNOR and the equality comparator
The gate that tests for sameness
An XNOR (exclusive-NOR) gate outputs 1 exactly when its two inputs are equal, so it is a one-bit equality detector. ANDing one XNOR per bit builds an equality comparator that reports whether two multi-bit numbers match.
Build it in the lab →XOR outputs
1 when its inputs differ. Invert that and you get a gate that outputs 1 when its inputs are the same, which turns out to be one of the most useful tests in a computer: are these two bits equal? That gate is XNOR (exclusive-NOR), and a row of them is how a CPU compares two numbers. You met it in passing in the truth tables lesson; here you build it and put it to work.XNOR is
NOT XOR: its output is 1 when the two inputs are equal (both 0 or both 1), and 0 when they differ. It is the exact complement of XOR, so every row of its truth table is XOR's flipped.| A | B | F |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
1 in the two rows where the inputs match (0,0 and 1,1) and 0 where they differ. Compare with XOR: every output bit is inverted. This is why XNOR is read as an equality test.F = ¬(A ⊕ B) = 1 exactly when A equals B
Building XNOR: XOR then NOT
Just as AND was NAND then NOT, and OR was NOR then NOT, XNOR is XOR then NOT. Feed your XOR's output into an inverter and the result is
1 whenever the inputs match. Both parts are already in your library, so this is one wire of work.- Place your saved XOR. Connect
AandBto its inputs; its output is1when the inputs differ. - Place your saved NOT. Feed the XOR output into it.
- The inverter flips the result, so the final output is
1exactly whenAequalsB. Label itF.
F = NOT (A XOR B). Open it in the lab and sweep all four input rows. F should be high only when A and B are the same, the opposite of plain XOR.Do not confuse XNOR with NOR, even though both start with 'N'. NOR is
1 only when both inputs are 0; XNOR is 1 when the inputs are equal, which includes both being 0 and both being 1. They agree on the 0,0 row and disagree on the 1,1 row: 1 NOR 1 = 0 but 1 XNOR 1 = 1. XNOR is the equality test; NOR is the all-zero test.From one bit to a number: the equality comparator
One XNOR tells you whether two single bits match. To compare two whole numbers, you need every bit pair to match at once. So compare each pair with its own XNOR, then AND all the XNOR outputs together: the AND is
1 only if every bit agreed. That circuit is an equality comparator, and EQ = 1 means the two numbers are equal.EQ = (A0 XNOR B0) ∧ (A1 XNOR B1) ∧ ... ∧ (A7 XNOR B7)
The figure below is a 2-bit version: two XNORs (one per bit pair) feeding an AND. Widen it to eight XNORs and an 8-input AND and it compares two whole bytes. Each XNOR asks 'do these two bits match?', and the AND insists every answer is yes.
EQ = (A0 XNOR B0) AND (A1 XNOR B1), each XNOR an XOR-then-NOT pair. Open it in the lab and set A1 A0 and B1 B0; EQ goes high only when the two 2-bit numbers are identical (for example A = 10 and B = 10), and low if any bit differs.Try it
On the 2-bit comparator, set
A1 A0 = 11 and B1 B0 = 10. What does each XNOR output, and what does EQ read? Then make the numbers equal and check EQ again.Answer
Build it in the lab ↗Bit 0:
A0 = 1, B0 = 0 differ, so that XNOR outputs 0. Bit 1: A1 = 1, B1 = 1 match, so that XNOR outputs 1. The AND of 0 and 1 is 0, so EQ = 0: the numbers are not equal (3 vs 2). Set B1 B0 = 11 so both numbers are 3: now both XNORs output 1, the AND is 1, and EQ = 1. Every bit pair must match for the whole comparison to be true.There is a second, related way a CPU checks equality without a dedicated comparator: subtract the two numbers and test whether the result is zero. That is the route the ALU takes, using its subtractor and a zero flag, because the hardware is already there. A standalone XNOR comparator is the direct way; subtract-and-check-zero is the reuse-what-you-have way. Both answer 'is
A equal to B?'.XNOR completes the family of two-input gates: AND, OR, NAND, NOR, XOR, and now XNOR. As an equality detector it shows up in comparators, address-match logic (does this address equal the one I am watching for?), and error checks. Next, the Arithmetic group spends XOR and AND on addition, and later the ALU uses equality, via subtraction, to let a program make decisions.
Frequently asked
What is an XNOR gate?
An XNOR (exclusive-NOR) gate outputs
1 exactly when its two inputs are equal (both 0 or both 1), and 0 when they differ. It is the complement of XOR and works as a one-bit equality detector.How do you build an XNOR gate?
Feed an XOR into a NOT:
F = NOT (A XOR B). This mirrors how AND is NAND then NOT and OR is NOR then NOT. Since XOR is 1 when the inputs differ, inverting it gives 1 when they match.What is the difference between XNOR and NOR?
NOR is
1 only when both inputs are 0; XNOR is 1 when the inputs are equal, which covers both being 0 and both being 1. They agree on 0,0 but differ on 1,1: 1 NOR 1 = 0, while 1 XNOR 1 = 1. XNOR tests for sameness; NOR tests for all-zero.How does a CPU check whether two numbers are equal?
You've got the theory. Now build it from scratch and watch it work.
Build it in the lab →Builds towardComparators