XNOR calculator: find matching bits
This XNOR calculator applies exclusive NOR, the inverse of XOR. Each output bit is set wherever the inputs match, whether both are 0 or both are 1, and cleared wherever they differ, so XNOR acts as a bit-level equality test. It is also called the equivalence gate.
XNOR truth table
| A | B | A XOR B | A XNOR B |
|---|---|---|---|
| 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Worked 8-bit example: 12 XNOR 10
00001100 (12)
XNOR 00001010 (10)
--------
= 11111001 (249, signed −7, 0xF9)
The numbers differ in bits 2 and 1, so those are 0 and every other bit is 1. As an unsigned byte that is 249; as a signed byte, −7. In 16 bits the result is 65529 (0xFFF9).
Reading the result
- Identical inputs give all 1s: 12 XNOR 12 is 255 in 8 bits, or −1 signed. That is a quick equality check.
- Counting zeros in the result gives the number of differing bits, the Hamming distance.
- Complement relationship: XNOR plus XOR always covers every bit exactly once, so 249 + 6 = 255 for these inputs in 8 bits.
Counting matching bits
Because every 1 in an XNOR result marks a position where the inputs agree, counting the ones measures similarity. For 12 XNOR 10 in 8 bits, 11111001 contains six ones: six bits match and two differ.
# Python 3.10+: mask to the width, then count
(~(12 ^ 10) & 0xFF).bit_count() # 6Binarized neural networks use exactly this trick. When values of +1 and −1 are stored as bits 1 and 0, the dot product of two n-bit vectors equals 2 × (number of matching bits) − n. For the example that is 2 × 6 − 8 = 4, computed with one XNOR and one population count instead of eight multiplications.
Three or more inputs
The calculator takes two operands. For several values, switch the operation to XOR, combine them two at a time and apply NOT to the final result. A bit then comes out as 1 when an even number of inputs have a 1 there, which is the even-parity check used in some error-detection schemes.
Where XNOR shows up
Hardware comparators use one XNOR gate per bit and AND the outputs to decide whether two words are equal. XNOR is also the basic operation in binarized neural networks, where it replaces multiplication of +1/−1 values.
Options
- Input base: decimal, hex, binary or octal, set separately for each operand; 0x, 0b and 0o prefixes override the setting for a value.
- Bit width: 8, 16, 32 or 64 bits. Because inversion sets unused high bits, always pick the width of your real data.
To see the differing bits directly, use the XOR calculator. The NOR calculator is a close cousin, and every operation is available in the bitwise calculator.