# Error Detection: Parity Checks & Parity Blocks

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 2: Data Transmission
- **Estimated Duration:** 15 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

During data transmission across physical wires, optical fibres, or radio frequencies, electromagnetic interference, power surges, and signal attenuation can cause bits to become corrupted. A `0` may accidentally flip to a `1`, or a `1` may flip to a `0`.

To prevent corrupted instructions or data from being processed, computer networks use **error detection** techniques.

---

## Learning Objectives

- Calculate and verify **Even** and **Odd** parity bits.
- Explain the operational mechanics and critical limitations of single-bit parity checks.
- Analyze a **2D Parity Block (Matrix Check)** to detect and automatically correct a flipped bit.

---

## Single-Bit Parity Checks

A **parity check** is one of the simplest methods used to check whether data has been corrupted during transmission. Before transmission begins, the sending and receiving devices must agree on the parity protocol: **Even Parity** or **Odd Parity**.

An extra bit—the **parity bit**—is appended to the data bits (typically placed as the Most Significant Bit / MSB or at the end).

### 1. Even Parity

- The parity bit is set to `0` or `1` so that the **total number of 1s in the byte is an EVEN number** ($0, 2, 4, 6, 8$).
- **Example A**: Data bits = `1010001`
- Count of `1`s: 3 (odd)
- To make the total count even, Parity Bit = `1`
- Transmitted byte: `11010001` (total of four 1s)
- **Example B**: Data bits = `1100011`
- Count of `1`s: 4 (already even)
- Parity Bit = `0`
- Transmitted byte: `01100011` (total of four 1s)

### 2. Odd Parity

- The parity bit is set so that the **total number of 1s in the byte is an ODD number** ($1, 3, 5, 7$).
- **Example A**: Data bits = `1010001`
- Count of `1`s: 3 (already odd)
- Parity Bit = `0`
- Transmitted byte: `01010001` (total of three 1s)
- **Example B**: Data bits = `1100011`
- Count of `1`s: 4 (even)
- Parity Bit = `1`
- Transmitted byte: `11100011` (total of five 1s)

### Verification at Receiver

When the receiver receives each byte, it counts the number of 1s:

- If Even Parity was agreed, but the received byte contains an odd number of 1s, an error is detected.
- If Odd Parity was agreed, but the received byte contains an even number of 1s, an error is detected.

---

## The Critical Limitation of Single-Bit Parity

While simple and computationally cheap, single-bit parity checks have two significant drawbacks:

1. **Undetected Errors (Even Number of Flips)**:

- If **two bits flip** during transmission (e.g. `1` flips to `0` AND `0` flips to `1`), the total count of `1`s remains the same!
- Any even number of bit errors ($2, 4, 6$) will fool the parity check, allowing corrupted data to pass undetected.

1. **Cannot Locate or Correct Errors**:

- Even when a single bit flips and parity fails, the receiver only knows *that* an error occurred somewhere in the byte; it has no way of knowing *which* of the 8 bits flipped. The entire byte must be discarded and retransmitted.

---

## Written practice and self-review

A communications channel uses parity checks to safeguard transmitted binary data.

1. Explain the **critical limitation** of a single-bit parity check if multiple bits flip during transmission.
2. Describe how a **2D Parity Block** (using horizontal row parity and vertical column parity) overcomes this limitation to detect and correct a single flipped bit.

Write an explanation before checking the theory. Check that each claim answers the question, uses the correct concept and explains why it applies. Discuss uncertain points with a teacher. The structured exercise below checks specific facts; it does not automatically mark this explanation.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Parity Blocks & 2D Parity (Multiple choice, 25 XP)

**Instructions:**
What is the primary advantage of using a 2D Parity Block (horizontal and vertical parity bits) compared to a simple single-byte parity check?

**Options:**
- **A**: It completely eliminates the need for encryption.
- **B**: It can both detect that a bit error occurred AND identify the exact row and column of the corrupted bit, allowing automatic correction.
- **C**: It compresses the transmitted file size by 50%.
- **D**: It ensures that even numbers are always converted into odd numbers.


**Autotest Expectations:**
- **Correct option selected**: `selection_match` -> Expected: `b`

### Exercise 2: Exam-Style Question: Calculating Parity Bits (Fill in blank, 35 XP)

**Instructions:**
Determine the required **parity bit** (to be placed as the Most Significant Bit) for each of the following 7-bit data bytes:

1. Data byte `1010001` using **Even Parity**: [blank_1]
2. Data byte `1100011` using **Even Parity**: [blank_2]
3. Data byte `0100100` using **Odd Parity**: [blank_3]


**Starter Code:**
```text
{"word_bank":["1","0","2","none"],"template":"1. Byte 1 parity bit: [blank_1]\n2. Byte 2 parity bit: [blank_2]\n3. Byte 3 parity bit: [blank_3]","prompt":"Exam-Style Question: Calculating Parity Bits","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```

**Autotest Expectations:**
- **Correct calculated parity bits**: `text_exact` -> Expected: `{&quot;blank_1&quot;:&quot;1&quot;,&quot;blank_2&quot;:&quot;0&quot;,&quot;blank_3&quot;:&quot;1&quot;}`

### Exercise 3: Application: Check the reasoning (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. Even parity: data 1011000 contains three ones. Parity bit: Odd parity for the same data: Can two flipped bits pass a single parity check: yes or no?


**Starter Code:**
```text
{"prompt":"Application: Check the reasoning","template":"1. Even parity: data 1011000 contains three ones. Parity bit: [blank_1]\n2. Odd parity for the same data: [blank_2]\n3. Can two flipped bits pass a single parity check: yes or no? [blank_3]","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}],"word_bank":["1","0","yes","no","2","never"]}
```


