AQA 7517, section 4.5.5.3
When data is transmitted or stored, bits can be changed by accident, for example by electrical interference. An error is when a received or stored bit differs from the original. We use extra data to detect errors, and sometimes to correct them.
Parity bits
A parity bit is one extra bit added to a group of bits so that the total number of 1s is even (even parity) or odd (odd parity). The sender and receiver agree which.
Even parity: the parity bit is chosen so the whole group has an even number of 1s.
| Data (7 bits) | Number of 1s | Parity bit | Sent (8 bits) |
|---|---|---|---|
1011001 |
4 | 0 | 01011001 |
1011101 |
5 | 1 | 11011101 |
Adding the even parity bit to 1011001:
The receiver counts the 1s. If the count is odd in even parity, an error has occurred.
- It can detect a single-bit error.
- It cannot say which bit is wrong, so it can't correct it. The data must be sent again.
- It misses errors where an even number of bits change (such as two bits), because the parity stays correct.
Example
Sent: 01011001 (even parity). Received: 01001001. There are three 1s, which is odd, so the receiver knows there was an error.
Majority voting
In majority voting, each bit is sent several times (usually 3). The receiver takes the value that appears most often for each bit.
| Original bit | Sent | Received | Majority | Correct? |
|---|---|---|---|---|
| 1 | 111 | 101 | 1 (two 1s) | Yes |
| 0 | 000 | 010 | 0 (two 0s) | Yes |
| 1 | 111 | 001 | 0 (two 0s) | No: two errors fooled it |
For example, receiving 111 010 110:
- It can correct a single-bit error in each group of three, not just detect it.
- It is inefficient: sending each bit three times triples the amount of data.
- It fails if two or more of the three copies are wrong.
Checksums
A checksum is a value calculated from a block of data and sent with it. The receiver calculates the checksum again from the data it received, and compares it with the one sent. If they differ, there has been an error.
A simple checksum adds up all the bytes and keeps the remainder when divided by 256:
def checksum(data):
return sum(data) % 256
block = [72, 101, 108, 108, 111] # the bytes of "Hello"
print(checksum(block)) # 244
received = [72, 101, 109, 108, 111] # one byte has changed
print(checksum(received)) # 245: different, so an error
- It can detect errors in a whole block of data, not only one byte.
- It detects errors but doesn't say where they are.
- It's used for network packets, downloaded files and storage.
- It isn't perfect: two errors might cancel each other out, but this is unlikely.
Check digits
A check digit is an extra digit added to the end of a number, calculated from the other digits. It is used to check that a number has been typed or scanned correctly.
Example: ISBN-13 (books)
Take the first 12 digits, multiply them alternately by 1 and 3, and add up the results. The check digit is the number that makes the total a multiple of 10.
For 978030640615 (the first 12 digits):
| Digit | 9 | 7 | 8 | 0 | 3 | 0 | 6 | 4 | 0 | 6 | 1 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 |
| Product | 9 | 21 | 8 | 0 | 3 | 0 | 6 | 12 | 0 | 18 | 1 | 15 |
The total is . The next multiple of 10 is 100, so the check digit is . The full ISBN is 9780306406157.
If one digit is mistyped, the sum won't be a multiple of 10 any more, and the error is caught. Check digits also catch many cases where two neighbouring digits are swapped. Barcodes and bank card numbers use check digits too.
Comparing the methods
| Method | What it does | Detects | Corrects |
|---|---|---|---|
| Parity bit | One extra bit per group | A single-bit error | No |
| Majority voting | Each bit sent several times | Errors | Yes (single errors in each group) |
| Checksum | A value calculated from a block | Errors in a block | No |
| Check digit | An extra digit calculated from a number | Typing and scanning errors | No |
Worked example
Task: use even parity. The receiver gets the bytes 10110011, 01100101 and 11000110. Which are wrong?
Count the 1s in each:
10110011: five 1s, which is odd. An error.01100101: four 1s, which is even. Passes the check.11000110: four 1s, which is even. Passes the check.
The first byte contains an error, but we don't know which bit. The data must be resent.
Exam tip: say whether each method detects or corrects, and mention its weakness: parity can't find which bit is wrong and misses an even number of errors, and majority voting multiplies the data size.
Check yourself
What is the even parity bit for 1100101?
There are four 1s, which is already even, so the parity bit is 0.
Which method can correct errors without asking for the data to be resent?
Majority voting, because each bit is sent several times and the most common value is used.
What is the purpose of a check digit?
To detect mistakes when a number is typed in or scanned, because it is calculated from the other digits and checked.
Why can parity checking miss some errors?
If two bits change, the number of 1s stays even (or odd), so the parity still appears to be correct.