C7 · Lesson 3 · Codes and parity
Gray code, BCD, and bits that catch and correct errors
Plain binary is the natural way to count, but it isn't always the right way to encode. A shaft encoder read in binary can report wild values as it turns; a decimal display wants digits, not powers of two; a bit flipped by noise in a memory or on a cable should be noticed, and better, fixed. A handful of codes handle all three, and they're built from the gate you already know best for the job: XOR.
Play
- The circuit converts a 4-bit binary number B to Gray code G and computes its parity P (1 when an odd number of bits are 1).
- Step B from 0 to 15 and watch G: exactly one bit changes at each step, even from 7 to 8, where binary changes all four.
- Watch P: it changes at every step of the Gray code, since each step flips one bit.
Predict
A 3-bit position sensor reports its angle in plain binary. As it turns from 3 (011) to 4 (100), its three bits don't switch at exactly the same instant. What might a reader that samples in between see?
Any value. All three bits change, and a sample taken while some have switched and others haven't can catch any combination: 000, 111, 101 and so on. A reading of 7 from a sensor moving between 3 and 4 is a serious error. In Gray code, neighbors differ in one bit, so a sample in between can only be one or the other.
Name it
Gray code
Neighboring values differ in exactly one bit. Conversion is a shift and an XOR:
Gray code is used for shaft and position encoders, for counters whose values cross clock domains (lesson 41), and to order Karnaugh map rows.
BCD
Binary-coded decimal stores each decimal digit in 4 bits: 59 is 0101 1001. Only 10 of the 16 codes are used, an efficiency of = 83%, but conversion to a display is trivial. Adding BCD digits in binary needs a correction: if a digit's sum exceeds 9, add 6 to skip the six unused codes, and carry.
Parity
An even parity bit is the XOR of the data bits, so the whole word always holds an even number of 1s. Any single flipped bit, indeed any odd number, makes the parity wrong. It can't say which bit, and two errors cancel.
Hamming codes
Placing several parity bits so that each checks a different subset of positions lets the pattern of failed checks, the syndrome, point at the bad bit. In the Hamming(7,4) code, with positions 1 to 7, parity bits sit at 1, 2 and 4, and parity bit checks every position whose number has bit j set. A single error at position p gives a syndrome equal to p. With r parity bits, k data bits can be protected if
The minimum distance d between codewords sets what a code can do: detect up to errors, or correct up to . Hamming codes have d = 3; adding an overall parity bit gives d = 4, correcting one error and detecting two (SECDED), as in ECC memory.
Worked example
Convert 13 to 4-bit Gray code and back.
Show the solution
- Shift right and XOR. 13 = 1101, and shifted right it's 0110:
- Convert back. The top bit is 1; then XOR in each Gray bit below it in turn: giving 1101 = 13 again.
- Why that works. Each binary bit is the XOR of all the Gray bits above and including it, so the conversion back is a running XOR from the top.
Answer 13 is 1011 in Gray code, and converting 1011 back gives 1101 = 13.
Worked example
Add the BCD digits 9 and 7.
Show the solution
- Add in binary. which is not a valid BCD digit and not the right display.
- Correct the sum. Because the sum exceeds 9, add 6: a carry of 1 into the tens digit and 0110 = 6 in the units: 16.
- Why 6. The correction skips the six codes, 1010 to 1111, that BCD never uses.
Answer 1 0110 in BCD: a 1 in the tens digit and 6 in the units, 16.
Worked example
Encode the data 1011 in Hamming(7,4), flip bit 6, and find the error.
Show the solution
- Compute the parity bits. With data bits = 1011 at positions 3, 5, 6 and 7:
- Assemble the codeword. The codeword, positions 1 to 7, is 0110011.
- Flip position 6. The word becomes 0110001.
- Recheck the parity. The parity over positions 1, 3, 5, 7 passes (0), the one over 2, 3, 6, 7 fails (1), and the one over 4, 5, 6, 7 fails (1).
- Read the syndrome. The syndrome is 110 in binary, 6: flip position 6 back and the data is recovered.
Answer the codeword is 0110011; with bit 6 flipped, the syndrome is 110 = 6, which points to the flipped bit.
Check
Convert the binary number 0111 to Gray code.
Check
Write the even-parity bit for the data .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A Hamming code protects k = 11 data bits.
(a)
How many parity bits does it need?
(b)
What's its code rate, data bits over total bits?
Check
A code has minimum distance d = 3.
(a)
How many errors can it detect?
(b)
How many can it correct?
Check
The BCD digits 7 and 6 are added.
(a)
What's the 5-bit binary result after the correction, as a number?
(b)
What's the units digit?
Check
Write hamming74_encode(d) taking a list of 4 data bits and returning the 7-bit codeword (positions 1 to 7, parity at 1, 2, 4), and hamming74_correct(c) returning (data, position): the corrected data bits and the position of the error it fixed (0 if none).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a Hamming code's syndrome point directly at the bit in error?
Each parity bit checks the positions whose binary number has a particular bit set: the first checks positions with bit 0 set, the second those with bit 1 set, and so on. A single error at position p upsets exactly the checks corresponding to the 1 bits in p's binary number, and no others. Reading the failed checks as a binary number therefore spells out p itself. That's why the parity bits sit at positions 1, 2, 4 and so on: each is the only position checked by its own parity, so it never disturbs the others.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.