Sub topic 2
Parity Checks and Two-Dimensional Parity
Assume you know binary numbers, XOR, and that the data link layer sends bits in frames. This chapter explains how parity adds controlled redundancy so a receiver can detect corrupted data.
Single-bit parity
A parity bit is one extra bit chosen to make the number of 1 bits follow a rule.
- Even parity: the total number of 1 bits, including the parity bit, is even.
- Odd parity: the total number of 1 bits is odd.
For example, the data 1011001 contains four 1 bits. Its even-parity bit is 0, so the transmitted sequence is 10110010. If one bit changes during transmission, the receiver counts the 1s and finds an odd total. It reports an error.
The receiver can detect every single-bit error with either parity rule. However, if two bits change, the parity may become valid again. Therefore, one-dimensional parity cannot detect every error.
def add_even_parity(data: str) -> str:
"""Return data followed by its even-parity bit."""
if any(bit not in "01" for bit in data):
raise ValueError("data must contain only 0 and 1")
parity_bit = str(data.count("1") % 2)
return data + parity_bit
def check_even_parity(frame: str) -> bool:
"""Return True when frame has a valid even parity."""
return frame.count("1") % 2 == 0
frame = add_even_parity("1011001")
print(frame) # 10110010
print(check_even_parity(frame)) # TrueTwo-dimensional parity
Two-dimensional parity arranges data into rows and columns. The sender adds one parity bit to every row and every column. The receiver checks both directions.
| Data row | Row parity |
|---|---|
| 1011 | 1 |
| 0100 | 1 |
| 1101 | 1 |
| 0010 | 1 |
For the four data columns, the column parity bits are 0, 0, 0, 0. A complete block therefore contains both row-parity and column-parity information.
If one data bit changes, exactly one row check and one column check fail. Their intersection identifies the damaged bit, so two-dimensional parity can correct one-bit errors, not merely detect them.
Two-dimensional parity detects every one-bit error and every two-bit error. It can also detect many burst errors because a burst usually affects several rows or columns. It does not detect all multi-bit patterns. For example, errors at the four corners of a rectangle can leave every affected row and column with an even number of errors.
> callout note > Parity indicates that corruption occurred. It does not prove that the data is correct, and a valid parity result does not guarantee that no error occurred.
Exercise
Exercise
A data word contains five 1 bits. What parity bit should be appended for even parity, and what happens if two bits then flip?
Show answer
Append 1 to make six 1 bits. If two bits flip, the number of 1 bits may remain odd or may return to an even total, so single parity cannot reliably detect the error.