CS With NehalCS With Nehal

Hamming Code Simulator

Encode data with Hamming (7,4) or (12,8), flip a bit and watch the parity checks find and correct it.

1. Data bits

Click to change the data. Parity bits go at positions that are powers of 2 (1, 2, 4, 8); data fills the rest.

D1
D2
D3
D4

2. Encoded codeword (even parity)

Hover a parity bit to see which positions it checks: every position whose binary number has that bit set.

001
0
P1
010
1
P2
011
1
D1
100
0
P4
101
0
D2
110
1
D3
111
1
D4
P1checks positions 1, 3, 5, 7= 1 ⊕ 0 ⊕ 1 → 0
P2checks positions 2, 3, 6, 7= 1 ⊕ 1 ⊕ 1 → 1
P4checks positions 4, 5, 6, 7= 0 ⊕ 1 ⊕ 1 → 0

3. Received codeword

Click one bit to flip it (a 1-bit transmission error).

1
2
3
4
5
6
7

Check P1: ones = 2 → even ✓ (0)

Check P2: ones = 4 → even ✓ (0)

Check P4: ones = 2 → even ✓ (0)

4. Locate & correct

0 = 0
Syndrome (failed checks added)
000
Syndrome in binary
Every check passes, so no error. The data bits are read from the non-parity positions.

Recovered data: 1011 ✓ matches the original

Hamming codes correct any single-bit error. With two flipped bits the syndrome points to the wrong place.

About This Simulation

This Hamming code simulator encodes 4 or 8 data bits by placing parity bits at positions that are powers of two. You can flip any bit of the received codeword and watch the parity checks combine into a syndrome that points to the exact bit to correct.

Features

  • Hamming (7,4) and Hamming (12,8) codes
  • Codeword with parity (P) and data (D) positions labelled
  • Hover a parity bit to see the positions it checks
  • Flip any bit to simulate a transmission error
  • Syndrome calculation, error location and corrected data

Topics covered

  • Hamming Code
  • Error Correction
  • Parity Bits
  • Syndrome
  • Forward Error Correction

Explanation

A Hamming code is an error-correcting code. It adds several parity bits so that a single-bit error can be both detected and located, letting the receiver correct it without asking for the data again. Each parity bit checks a different, overlapping group of positions.

How it works

  1. Number the bit positions from 1. Positions that are powers of two (1, 2, 4, 8) hold parity bits; the rest hold data.
  2. Parity bit P1 checks every position whose binary number ends in 1 (1, 3, 5, 7, …); P2 checks positions with the 2s bit set (2, 3, 6, 7, …); P4 checks positions with the 4s bit set, and so on.
  3. Each parity bit is set so that its group has even parity.
  4. The receiver recalculates every check. The positions of the failed checks are added together to give the syndrome.
  5. A syndrome of 0 means no error; otherwise it is the position of the wrong bit, which is flipped to correct it.

Why it matters

Hamming codes are the basis of error-correcting (ECC) memory and are covered in computer networks and data communications courses. They show how carefully placed redundancy allows forward error correction.

Frequently Asked Questions

How many parity bits does a Hamming code need?

With r parity bits you can protect up to 2ʳ − r − 1 data bits. 3 parity bits protect 4 data bits (7,4); 4 parity bits protect up to 11 data bits.

Can Hamming codes correct two errors?

No. A basic Hamming code corrects one error. With two errors the syndrome points to the wrong bit. An extra overall parity bit (SECDED) can detect, but not correct, two errors.

What is the syndrome?

The sum of the positions of the parity checks that fail. It gives the position of the incorrect bit, or 0 if there is no error.

Related Simulations