networking · beginner · ~15 min
Compute a one-byte XOR check.
Compute a one-byte XOR checksum (BCC) — the cheap parity check many serial protocols use.
Implement unsigned char xor_checksum(const unsigned char *data, int n) that returns the XOR of all n bytes.
data: the byte buffer.n: number of bytes.The XOR of every byte, as an unsigned char.
{0x01, 0x02, 0x03} -> 0x00 (1 ^ 2 ^ 3)
{0xFF, 0x0F} -> 0xF0
data: the byte buffer; n: the byte count.
The XOR of all n bytes, as an unsigned char.
XOR every byte into a single-byte accumulator.
unsigned char xor_checksum(const unsigned char *data, int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.