📘 CodingMarble Learn

Error-Correcting Codes: How Computers Catch and Fix Mistakes

Noise can flip bits while data travels. A parity bit makes the number of 1s even, so one flipped bit can be detected. Row and column parity can also find and fix a single wrong bit. Extra check bits make the message longer, so transmission time (bits ÷ speed) goes up.

🎬 Step-by-step story

  1. A message is a row of bits. Tall blue block is 1, flat block is 0. The sender sends 1 0 1 1 and the receiver gets 1 0 1 1.
  2. Noise on the wire flips one bit. The receiver gets 1 1 1 1 and does not know that anything went wrong.
  3. Fix: add one extra bit, the parity bit. Choose it so the number of 1s is even. Now the message has a check built in.
  4. The receiver counts the 1s again. Five is odd, so a bit was flipped. The error is detected, but we do not know which bit.
  5. Put bits in a grid with a check bit for every row and column. The wrong bit is where the odd row and the odd column cross. Flip it back.
  6. Free play: flip any bit and watch the grid find it. Change the speed and see how the extra check bits change the transmission time.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why can bits change on the way?

Noise such as heat, lightning, a scratch or a weak signal can disturb the pulses. The receiver may read a 0 as a 1. Watch the red block in the 3D.

Why add an extra bit if it carries no new information?

It is like a spell-check for the message. The extra bit does not say anything new, but it lets the receiver check the data. See how the parity bit made the count even.

Does the parity bit always have to be 1?

No. It is 1 only when the data has an odd number of 1s. If the data already has an even number of 1s, the parity bit is 0.

The receiver found an error. Why can it not fix it?

One parity bit gives only odd or even. Every bit could be the wrong one. The receiver needs to ask for a re-send, or the sender must use a code with more check bits.

How does the grid find the exact wrong bit?

A wrong bit makes its own row and its own column odd. Only one cell lies in both. The pink strips cross exactly on the wrong red block.

What if the wrong bit is a check bit itself?

Then only one row or only one column turns odd. The receiver knows that a check bit is wrong, not the data. Slide the first slider to bits 4, 8, 12 or 13 to see this.

Why does safety cost time?

Check bits are extra bits and every bit takes time to travel. Time = bits ÷ speed. Move the speed slider and compare 16 bits with 9 bits.

Why do errors happen?

Bits travel as electric pulses, light flashes or radio waves. Noise (heat, lightning, a scratch on a disc, weak signal) can turn a 0 into a 1 or a 1 into a 0. A small change can be a big problem: 1 0 1 1 is 11, but 1 1 1 1 is 15.

Two ideas help us:

Both need redundancy: extra bits that carry no new data, only checking information.

Parity bit: detecting one error

With even parity we add one bit so that the total number of 1s (data plus parity bit) is even.

The receiver counts the 1s. Even means "looks fine". Odd means "an error!" (With odd parity the rule is the opposite: total must be odd.)

Limits: one parity bit catches 1, 3, 5 ... flipped bits (an odd number) but misses 2, 4 ... flips, because the count becomes even again. It also cannot tell which bit is wrong, so the message must be sent again.

Row and column parity: finding and fixing

Arrange the data bits in a grid. Add a parity bit to the end of every row and the bottom of every column (and one corner bit). If one bit is wrong, exactly one row and one column become odd. The wrong bit is where they cross, so the receiver flips it back. This is a simple error-correcting code.

For a grid of r rows and c columns of data, the message carries r × c data bits and (r + 1)(c + 1) − r × c check bits.

Hamming code: smarter check bits

Richard Hamming made a code where each check bit looks at a different group of positions. The check bits sit at positions 1, 2, 4, 8 ... and together they point to the exact position of a single wrong bit (the failed checks, added up, give the position number). With m data bits we need r check bits where 2r ≥ m + r + 1. For 4 data bits r = 3, so the famous Hamming (7,4) code has 7 bits in all. It corrects any single flipped bit using far fewer check bits than a big grid.

Transmission time

The speed of a channel is measured in bits per second (bit/s). The time to send a message is

time = number of bits ÷ speed

Check bits are also sent, so they add time. Sending 16 bits (9 data + 7 check) at 8 bit/s takes 2 s; the 9 data bits alone would take 1.125 s. If the receiver finds an error and asks for a re-send, the message goes twice and the time doubles. A detecting code (cheap) plus occasional re-send, or a correcting code (more bits but no re-send), is a trade-off between time and safety.

Remember the units: 1 byte = 8 bits, and 1 kbit/s = 1000 bit/s.

Try it

Try it with 7 friends. Each friend holds up a card with 0 or 1. Count the 1s: you need an even count, so add an eighth friend (parity) who holds up 0 or 1 to make it even. Now let one friend secretly turn the card over. Count again: odd! You found the error. Now arrange 9 friends in a 3 × 3 grid with row and column helpers and find the flipped card by seeing which row and which column are odd. In the 3D above, use the first slider to flip any of the 16 bits and read what the grid says.

Key formulas and definitions

Worked examples

1. Find the even-parity bit for the data 1 1 0 1 0 0 1.

Count the 1s: 1 + 1 + 0 + 1 + 0 + 0 + 1 = 4 ones. That is already even, so the parity bit is 0. Message: 1 1 0 1 0 0 1 0.

2. A receiver gets 1 0 1 1 0 1 0 1 with even parity (last bit is the parity bit). Is there an error?

Count the 1s: 1 + 0 + 1 + 1 + 0 + 1 + 0 + 1 = 5. Five is odd, but even parity needs an even count. So an error is detected (an odd number of bits flipped).

3. Data 1 0 1 1 was sent with parity bit 1 (so 1 0 1 1 1). Two bits flipped and the receiver got 0 1 1 1 1. Does the parity check notice?

The 1s in 0 1 1 1 1 number 4, which is even. The check misses the error, because two bits flipped (the first 1 became 0 and the second 0 became 1). A single parity bit misses an even number of flips. If only the first bit had flipped (0 0 1 1 1), the count would be 3, odd, and the error would be found.

4. How long does it take to send 1 200 bits at 300 bit/s?

time = bits ÷ speed = 1200 ÷ 300 = 4 s.

5. A file of 600 bytes is sent at 2 400 bit/s. How long does it take? How long if a parity bit is added to every byte?

600 bytes = 600 × 8 = 4800 bits. Time = 4800 ÷ 2400 = 2 s. With a parity bit per byte each byte becomes 9 bits: 600 × 9 = 5400 bits. Time = 5400 ÷ 2400 = 2.25 s. The check costs 0.25 s.

6. A 3 × 3 grid of data uses row and column parity (with a corner bit). In the received grid, row 3 and column 1 are odd. Where is the wrong bit, and what do you do?

The wrong bit is where row 3 and column 1 cross: row 3, column 1. Flip that bit back (0 to 1 or 1 to 0). The code has corrected the error, with no re-send.

7. How many check bits does a Hamming code need for 11 data bits?

We need 2^r ≥ m + r + 1 with m = 11. Try r = 3: 8 ≥ 15? No. Try r = 4: 16 ≥ 16? Yes. So r = 4 check bits; the code word is 15 bits long (Hamming (15,11)).

Common mistakes

Practice quiz

1. What does an even-parity bit make even?
2. Data 1 1 1 0 needs which even-parity bit?
3. A single parity bit can NOT detect:
4. Send 2 000 bits at 500 bit/s. Time taken:
5. In row and column parity, a wrong data bit is found:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between error detection and error correction?

Detection tells you that a mistake happened. Correction also tells you which bit is wrong and fixes it, so the data does not have to be sent again.

What is a parity bit?

An extra bit added to data so that the total number of 1s is even (even parity) or odd (odd parity). The receiver counts the 1s to check the message.

Why do check bits increase transmission time?

They are extra bits that must also travel. Transmission time = number of bits ÷ speed, so more bits means more time. It is the price paid for safety.

Learn first

Learn next

Related lessons

All Computer Science lessons