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

Group: Gates
URL: https://digiwleea.wleeaf.dev/learn/xnor/

[XOR](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/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 |

_XNOR is 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 = NOT (A XOR B) = 1 exactly when A equals B
```

## Building XNOR: XOR then NOT

Just as [AND](https://digiwleea.wleeaf.dev/learn/and/) was [NAND](https://digiwleea.wleeaf.dev/learn/nand/) then [NOT](https://digiwleea.wleeaf.dev/learn/not/), and [OR](https://digiwleea.wleeaf.dev/learn/or/) was [NOR](https://digiwleea.wleeaf.dev/learn/nor/) then NOT, XNOR is [XOR](https://digiwleea.wleeaf.dev/learn/xor/) then [NOT](https://digiwleea.wleeaf.dev/learn/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.

1. Place your saved [XOR](https://digiwleea.wleeaf.dev/learn/xor/). Connect `A` and `B` to its inputs; its output is `1` when the inputs differ.
2. Place your saved [NOT](https://digiwleea.wleeaf.dev/learn/not/). Feed the XOR output into it.
3. The inverter flips the result, so the final output is `1` exactly when `A` equals `B`. Label it `F`.

_Circuit diagram: XNOR built as an XOR feeding a NOT: 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._

> **WARN:** Do not confuse XNOR with [NOR](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/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) AND (A1 XNOR B1) AND ... AND (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.

_Circuit diagram: A 2-bit equality comparator: 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._

**Q (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.

**A:** 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.

> **TIP:** 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](https://digiwleea.wleeaf.dev/learn/alu/) takes, using its [subtractor](https://digiwleea.wleeaf.dev/learn/subtract/) 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`?'.

> **KEY:** 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](https://digiwleea.wleeaf.dev/learn/halfadder/) group spends XOR and AND on addition, and later the [ALU](https://digiwleea.wleeaf.dev/learn/alu/) uses equality, via subtraction, to let a program make decisions.

### FAQ

**Q:** What is an XNOR gate?

**A:** 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](https://digiwleea.wleeaf.dev/learn/xor/) and works as a one-bit equality detector.

**Q:** How do you build an XNOR gate?

**A:** Feed an [XOR](https://digiwleea.wleeaf.dev/learn/xor/) into a [NOT](https://digiwleea.wleeaf.dev/learn/not/): `F = NOT (A XOR B)`. This mirrors how [AND](https://digiwleea.wleeaf.dev/learn/and/) is [NAND](https://digiwleea.wleeaf.dev/learn/nand/) then NOT and [OR](https://digiwleea.wleeaf.dev/learn/or/) is [NOR](https://digiwleea.wleeaf.dev/learn/nor/) then NOT. Since XOR is `1` when the inputs differ, inverting it gives `1` when they match.

**Q:** What is the difference between XNOR and NOR?

**A:** 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.

**Q:** How does a CPU check whether two numbers are equal?

**A:** One way is an **equality comparator**: compare each bit pair with an XNOR, then [AND](https://digiwleea.wleeaf.dev/learn/and/) all the results, so the output is `1` only if every bit matches. Another way, the one an [ALU](https://digiwleea.wleeaf.dev/learn/alu/) uses, is to [subtract](https://digiwleea.wleeaf.dev/learn/subtract/) the numbers and check whether the result is zero, since `A - B = 0` means `A` equals `B`.
