Simvik

XOR checksum

Compute the XOR of every byte in a buffer. An easy embedded C problem (Protocols, framing and checksums), run and graded in the browser.

easy · Protocols, framing and checksums · checksum

The simplest integrity check, used by NMEA GPS sentences and many small serial protocols, is the XOR of all the bytes.

Return the XOR of the n bytes at data. An empty buffer gives 0.

Starter code

uint8_t xor_checksum(const uint8_t *data, size_t n) {
  return 0;
}

Example tests

Three bytes

const uint8_t d[] = { 0x01, 0x02, 0x04 };
printf("0x%02X\n", xor_checksum(d, 3));

Expected output:

0x07

Identical bytes cancel

const uint8_t d[] = { 0xFF, 0xFF };
printf("0x%02X\n", xor_checksum(d, 2));

Expected output:

0x00

Learn it first

More Protocols, framing and checksums problems