Lesson 39: Parity check
Did the data arrive safely?
- Data sent down a wire or through the air can change on the way: a 0 becomes a 1.
- A parity check finds many of these errors. The sender adds one extra bit, the parity bit, so the number of 1s is always even (even parity) or always odd (odd parity).
- The receiver counts the 1s. With even parity, an odd count means an error.
Words for this lesson
| Word | Meaning |
|---|---|
| bit | one binary digit: 0 or 1 |
| byte | 8 bits |
| parity bit | the extra bit that makes the count of 1s even (or odd) |
| error | a bit that changed on the way |
Even parity
| 7 data bits | 1s | Parity bit | Byte sent |
|---|---|---|---|
| 1100011 | 4 | 0 | 01100011 |
| 1000000 | 1 | 1 | 11000000 |
- If one bit changes on the way, the byte has an odd number of 1s and the receiver knows.
- If two bits change, the count is even again: the error is not found.
Count the 1s
void setup() {
Serial.begin(9600);
String bits = "1011";
int ones = 0;
for (int i = 0; i < bits.length(); i++) {
if (bits[i] == '1') {
ones = ones + 1;
}
}
Serial.println(ones);
}
void loop() {
}
- The Monitor shows 3.
In the exam
- 0478/12/F/M/23 Q5(c): (i) the bytes are sent with an even parity check: complete the parity bit for each byte. Count the 1s in the other 7 bits and choose 0 or 1 so the total is even, as your first program does. (ii) A parity block check also adds a parity byte for each column of bits, so it can find which bit changed and can find some errors that a byte check misses.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
Type 7 bits into the Serial Monitor, for example 1100011. Count the 1s. With even parity the total number of 1s must be even, so the parity bit is 1 when the count is odd and 0 when it is even. Print Send: , then the parity bit, then the 7 bits, for example Send: 01100011.
Clique em Executar para ver a saída aqui.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
Now be the receiver. Type a whole byte of 8 bits (parity bit first). Count its 1s: if the count is even, print OK and light the green LED (pin 12); if it is odd, a bit was changed on the way: print Error and light the red LED (pin 13). Only one LED is on at a time.
Clique em Executar para ver a saída aqui.