Sub topic 2
Parity Checks and Two-Dimensional Parity
This chapter assumes you know binary numbers, XOR, and the idea that the data link layer must notice corrupted frames. It builds on the previous chapter's distinction between single-bit and burst errors. Parity is the simplest error-detecting code, and the 2D version is the first code in this tutorial that can also correct an error.
Single-bit parity
A parity bit is one extra bit appended to a block of data so that the total number of 1s follows a fixed rule.
- Even parity: the total number of 1s, including the parity bit, is even.
- Odd parity: the total number of 1s, including the parity bit, is odd.
The sender computes the parity bit as the XOR of all data bits (even parity) or its complement (odd parity). The receiver counts the 1s in the whole received word. If the count breaks the rule, the receiver knows the word is corrupted.
For example, the data 1011001 has four 1s, so the even-parity bit is 0 and the sent word is 10110010. If one bit flips, the count becomes odd and the receiver detects it.
What single parity can and cannot do
- It detects every error that flips an odd number of bits (1, 3, 5, ...).
- It misses every error that flips an even number of bits, because the count stays even.
- It cannot say which bit is wrong, so it cannot correct anything.
- Its overhead is 1 bit per block. For 7 data bits this is 1/8 = 12.5%.
Two-dimensional parity
Two-dimensional (2D) parity arranges the data in a grid of r rows and c columns. It adds one parity bit to every row and one to every column. A final corner bit covers the row of column parities. Even parity is used throughout in the example below.
The check bits cost r + c + 1 bits for r × c data bits.
Worked example
Take three rows of four data bits: 1011, 0110, 1100.
| Row | Data | Row parity | Sent row |
|---|---|---|---|
| 1 | 1011 | 1 | 10111 |
| 2 | 0110 | 0 | 01100 |
| 3 | 1100 | 0 | 11000 |
| Column parity | 0001 | 1 | 00011 |
Column 1 holds 1, 0, 1, so its parity is 0. Column 4 holds 1, 0, 0, so its parity is 1. The corner bit is 1, which is both the parity of the row-parity column (1, 0, 0) and of the column-parity row (0, 0, 0, 1).
Now suppose bit 3 of row 2 flips, so 01100 arrives as 01000.
- Row 2 now has one 1, which is odd, so row 2 fails its check.
- Column 3 now holds 1, 0, 0, 0, which is odd, so column 3 fails its check.
- The failing row and failing column meet at exactly one bit. The receiver flips it back and recovers the data.
Code
This Python program builds the block, locates a single error, and shows an error pattern that gets past the check.
def encode(rows):
out = [r + str(r.count("1") % 2) for r in rows]
cols = "".join(
str(sum(int(r[j]) for r in out) % 2) for j in range(len(out[0]))
)
return out + [cols]
def failing(block):
bad_rows = [i for i, r in enumerate(block) if r.count("1") % 2]
bad_cols = [
j for j in range(len(block[0]))
if sum(int(r[j]) for r in block) % 2
]
return bad_rows, bad_cols
def flip(block, positions):
grid = [list(r) for r in block]
for i, j in positions:
grid[i][j] = "1" if grid[i][j] == "0" else "0"
return ["".join(r) for r in grid]
block = encode(["1011", "0110", "1100"])
print(block) # ['10111', '01100', '11000', '00011']
one_error = flip(block, [(1, 2)])
print(failing(one_error)) # ([1], [2]) -> correct bit (1, 2)
rectangle = flip(block, [(0, 0), (0, 1), (1, 0), (1, 1)])
print(failing(rectangle)) # ([], []) -> undetectedDetection and correction power
2D parity has minimum Hamming distance 4. Chapter 3 defines Hamming distance in detail. For now, it means any two valid blocks differ in at least 4 bit positions. This gives the following behavior.
| Error pattern | Result |
|---|---|
| 1 bit | Detected and corrected (one row and one column fail) |
| 2 bits in one row | Detected (two columns fail, no row fails), not correctable |
| 2 bits in different rows and columns | Detected (two rows and two columns fail), not correctable |
| 3 bits | Always detected |
| 4 bits at the corners of a rectangle | Undetected |
To see why every 1-, 2-, or 3-bit error is detected, suppose an error goes unnoticed. Then every row and every column must contain an even number of flipped bits. Each flipped bit sits in a row and a column that therefore need at least one more flip each. The smallest such pattern is a rectangle of 4 flips in 2 rows and 2 columns. So any pattern with fewer than 4 flips is caught.
Distance 4 satisfies d ≥ t + s + 1, so the code can correct t = 1 error while detecting s = 2. In practice, a receiver that corrects a single error should treat patterns with more than one failing row or column as uncorrectable.
Burst errors
If the grid is sent row by row, a burst no longer than the row width (including the row parity bit) touches each column at most once. Each affected column then has exactly one flip and fails its parity check. So these bursts are always detected, which is much better than single parity. Longer bursts can still produce the rectangle pattern.
Common mistakes
- Counting only data bits. The parity bit is part of the word, so the receiver counts it too.
- Mixing up odd and even rules. Fix the convention before computing. GATE questions usually state it.
- Claiming 2D parity detects all even-bit errors. It does not. A rectangle of 4 flips passes.
- Correcting after a multi-bit error. Only the pattern with one failing row and one failing column is safely correctable.
Exercise
A 2D even-parity scheme sends 8 rows of 8 data bits, with row parities, column parities, and a corner bit. (a) How many bits are sent in total, and what fraction are check bits? (b) A receiver finds that rows 3 and 5 and columns 2 and 6 fail. Can it correct the error?
Show answer
(a) The grid is 9 × 9 = 81 bits. Check bits are 8 + 8 + 1 = 17, so the fraction is 17/81, about 21%. (b) No. Two rows and two columns fail, so there are at least two flipped bits, and the intersections (3,2), (3,6), (5,2), (5,6) are all candidates. For example, flips at (3,2) and (5,6) produce the same failures as flips at (3,6) and (5,2). The receiver can only report detection and request retransmission.
Exercise
Why does single-bit even parity fail to detect a two-bit error, but 2D parity detects it?
Show answer
Two flipped bits change the 1-count of a single word by 0 or 2, so the word's parity still looks correct. In 2D parity, two flips in different rows leave two rows with odd counts, and two flips in the same row leave two columns with odd counts. Either way, at least one row or column check fails.