C73 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Gray code

Neighboring values differ in exactly one bit. Conversion is a shift and an XOR:

G=B⊕(B≫1),Bn−1=Gn−1,Bi=Bi+1⊕Gi.

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 log2⁡104 = 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 2j 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

2r≥k+r+1.

The minimum distance d between codewords sets what a code can do: detect up to d−1 errors, or correct up to ⌊d−12⌋. 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
  1. Shift right and XOR. 13 = 1101, and shifted right it's 0110: 1101⊕0110=1011
  2. Convert back. The top bit is 1; then XOR in each Gray bit below it in turn: 1⊕0=11⊕1=00⊕1=1 giving 1101 = 13 again.
  3. 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
  1. Add in binary. 1001+0111=10000=16 which is not a valid BCD digit and not the right display.
  2. Correct the sum. Because the sum exceeds 9, add 6: 10000+0110=10110 a carry of 1 into the tens digit and 0110 = 6 in the units: 16.
  3. 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
  1. Compute the parity bits. With data bits d1d2d3d4 = 1011 at positions 3, 5, 6 and 7: p1=d1⊕d2⊕d4=0p2=d1⊕d3⊕d4=1p4=d2⊕d3⊕d4=0
  2. Assemble the codeword. The codeword, positions 1 to 7, is 0110011.
  3. Flip position 6. The word becomes 0110001.
  4. 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).
  5. 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 B3B2B1B0.

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?

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.