Parity Generator and Checker
A parity circuit adds or checks a single bit that makes the number of ones in a word even or odd, catching single-bit errors.
What parity is
Parity is the simplest error-detecting code. A parity bit is appended to a data word so that the total number of 1 bits is even, called even parity, or odd, called odd parity. The receiver recomputes parity and compares.
Generating the bit
The parity of a word is the XOR of all its bits. For even parity the generated bit equals that XOR, so the total count becomes even. For odd parity the bit is the complement. A tree of XOR gates computes this with logarithmic depth.
Checking
At the receiver, XORing all the received data bits together with the received parity bit yields 0 if parity is intact and 1 if it is violated. A violation signals that an error occurred somewhere in the word.
What it catches and misses
A single parity bit detects any odd number of bit errors, including the common single-bit error, but cannot detect an even number of errors and cannot locate or correct any error. For correction, more powerful codes with multiple check bits are needed.
Uses
Parity protects memory words, serial links, and buses where a lightweight integrity check is enough. Its low cost, one bit and a tree of XORs, makes it ubiquitous as a first line of defense.
In code
parity_bit = 0
for b in data_bits:
parity_bit ^= b # even parity