When data is stored or sent over a wire, noise may change a 0 into a 1. A system that only holds the data cannot find out. Error codes add extra bits so that the receiver can detect an error, and in better codes, locate and correct it.

Parity Bit

A parity bit is one extra bit added so that the total number of 1s is even (even parity) or odd (odd parity). The receiver counts the 1s. If the count has the wrong parity, an error has occurred. A single parity bit detects any single-bit error but cannot say where the error is, and it misses errors in which two bits change.

Definition: Parity bit. An extra bit added to a group of data bits so that the total number of 1s in the group is even or odd, as agreed.

Hamming Distance

The Hamming distance between two code words is the number of positions in which they differ. A code with minimum distance d can detect d - 1 errors and correct up to (d - 1) / 2 errors, rounded down. The Hamming code has a minimum distance of 3, so it corrects one error.

Hamming Code

In the Hamming (7,4) code, four data bits are protected by three parity bits placed at positions 1, 2 and 4 of a seven-bit word. Each parity bit checks a set of positions, as shown in Table 1.10.

Table 1.10: Positions checked by each parity bit

Parity bit Position Positions checked
p1 1 1, 3, 5, 7
p2 2 2, 3, 6, 7
p4 4 4, 5, 6, 7

At the receiver the three checks are recomputed. The three results, read as a binary number c4 c2 c1, give the position of the wrong bit. A result of 000 means no error.

Example 1.11: Encoding and correcting with the Hamming code

Problem: Encode the data bits 1011 using even parity, then show how an error in position 5 is found.

Solution:

  1. Positions 3, 5, 6, 7 hold d1, d2, d3, d4 = 1, 0, 1, 1.
  2. p1 checks positions 1, 3, 5, 7: 1, 0, 1 have two 1s, so p1 = 0.
  3. p2 checks 2, 3, 6, 7: 1, 1, 1 have three 1s, so p2 = 1.
  4. p4 checks 4, 5, 6, 7: 0, 1, 1 have two 1s, so p4 = 0.
  5. The code word is 0110011. If position 5 changes to 1, the word is 0110111 and the checks give c1 = 1, c2 = 0, c4 = 1.

Result: c4 c2 c1 = 101, which is 5, so bit 5 is wrong and is flipped back.

w = [0, 1, 1, 0, 0, 1, 1]
w[4] ^= 1
c1 = w[0] ^ w[2] ^ w[4] ^ w[6]
c2 = w[1] ^ w[2] ^ w[5] ^ w[6]
c4 = w[3] ^ w[4] ^ w[5] ^ w[6]
print(c4, c2, c1, "error position", 4*c4 + 2*c2 + c1)

Output

1 0 1 error position 5

Common mistakes

  • Believing that a single parity bit can correct an error. It can only detect one.
  • Placing Hamming parity bits at positions 3, 5 and 6 instead of 1, 2 and 4.
  • Mixing up even and odd parity when counting the 1s.

Key takeaways

  • A parity bit detects a single-bit error but cannot locate it.
  • Hamming distance counts differing positions between two code words.
  • The Hamming code corrects any single-bit error.
  • The check result c4 c2 c1 gives the position of the wrong bit.

Practice questions

  1. What is even parity?
  2. What is the Hamming distance between 1011 and 1110?
  3. Where are the parity bits placed in the Hamming (7,4) code?

Answers to the practice questions

  1. Adding a bit so that the total number of 1s in the word is even.
  2. Two, because they differ in two positions.
  3. At positions 1, 2 and 4.

Sources

  • Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit I