Parity adds one extra bit to each group of bits so that the number of 1s is always even (even parity) or always odd (odd parity). The receiver counts the 1s again. If the count has the wrong parity, an error has occurred.
This lesson builds on serial and parallel transfer and on the binary patterns from representing numbers and text.
How do you work out a parity bit?
- Count the 1s in the data bits.
- For even parity, make the total even. If the count is odd, the parity bit is 1. If it is even, the parity bit is 0.
- For odd parity, make the total odd. If the count is even, the parity bit is 1. If it is odd, the parity bit is 0.
- Attach the parity bit to the data. Agree in advance where it goes, and in this lesson it goes at the end.
Worked example
Send the 7-bit pattern 1011001 using even parity.
Step 1, count. The bits are 1,0,1,1,0,0,1. The 1s number 4.
Step 2, decide. 4 is already even, so the parity bit is 0.
Step 3, transmit. The byte sent is 10110010.
Step 4, receive without error. The receiver counts 1s in 10110010 and again gets 4, which is even. The check passes.
Step 5, one bit flips. Suppose the byte arrives as 10010010. The 1s now number 3, which is odd, so the receiver reports an error.
Here is the same counting in Cambridge-style pseudocode for the 7 data bits:
count ← 0
FOR i ← 1 TO 7
IF Bits[i] = 1 THEN
count ← count + 1
ENDIF
NEXT i
IF count MOD 2 = 0 THEN
parity ← 0
ELSE
parity ← 1
ENDIF
Trace for 1011001: after i=1 count is 1, i=2 stays 1, i=3 gives 2, i=4 gives 3, i=5 stays 3, i=6 stays 3, i=7 gives 4. Then 4 MOD 2 = 0, so parity = 0. This matches the hand count.
What mistake do students make?
A common conclusion is: “The parity check passed, so the data is correct.”
That is not safe. Take the byte 10110010 above and flip two bits, the second and the fourth, to get 11100010. The 1s still number 4, so the check passes even though the data is wrong. The correction is to write: “Parity check passed, so no error was detected, but an even number of changed bits could still be hidden.”
Check yourself
1. Find the even parity bit for 1110110.
Show answer
1s: 1,1,1,0,1,1,0 gives 5. Five is odd, so the parity bit must be 1. The full byte is 11101101, which has 6 ones, an even number.
2. Odd parity is used. A byte arrives as 01100110. Is an error detected?
Show answer
1s: 0,1,1,0,0,1,1,0 gives 4, which is even. Odd parity needs an odd count, so an error is detected.
3. Explain why two flipped bits might not be detected.
Show answer
Each flip changes the count of 1s by one. Two flips can cancel out, so the count has the same parity and the check appears to pass.
Where this leads next
Parity is the simplest check. The next lesson looks at a checksum that covers a whole block of data. You can test your counting in the practice set or step through the loop above in the restricted pseudocode trace trainer.
If your counting is accurate but the wording of explanations still loses marks, our teachers can help in online one-to-one Computer Science tuition.