networking · intermediate · ~25 min

Compute CRC-8 over a byte stream (poly 0x07)

Bit-by-bit polynomial division — the heart of every CRC.

Challenge

Compute a CRC-8 checksum over a byte stream, bit by bit. This is the same loop that 1-Wire sensors, USB Power Delivery, and CAN bus frames use.

Task

Implement unsigned char crc8(const unsigned char *data, size_t len) using polynomial 0x07, initial value 0x00, and no final XOR-out.

The algorithm:

  1. Start with crc = 0.
  2. For each of the len bytes b in data:
    • crc ^= b.
    • Repeat 8 times: if the high bit of crc is set, crc = (crc << 1) ^ 0x07; otherwise crc = crc << 1. Keep crc masked to 8 bits.
  3. Return crc.

Input

  • data: the byte buffer to checksum.
  • len: number of bytes to process.

Output

The final CRC-8 value as an unsigned char.

Example

crc8("123456789", 9)  ->  0xF4     (canonical CRC-8 check value)
crc8("", 0)           ->  0x00
crc8("\x01", 1)       ->  0x07
crc8("A", 1)          ->  0xC0     ('A' is 0x41)

Edge cases

  • An empty buffer (len == 0) returns 0x00.

Rules

  • No lookup tables — use the bit-by-bit loop.

Why this matters

CRC checksums are everywhere: 1-Wire / I²C / CAN / SMBus all use CRC-8, and TCP/Ethernet use larger CRCs with the same core algorithm. Writing one by hand demystifies what looks like a magic value in protocol specs.

Input format

data, the byte buffer; len, the number of bytes to process.

Output format

The final CRC-8 value as an unsigned char (poly 0x07, init 0x00, no XOR-out).

Constraints

No lookup tables; bit-by-bit loop; keep crc masked to 8 bits.

Starter code

#include <stddef.h>
unsigned char crc8(const unsigned char *data, size_t len) { /* TODO */ return 0; }

Common mistakes

Forgetting to mask crc back to 8 bits after the shift. Using the wrong polynomial. Initialising with 0xFF (some CRC-8 variants do; this one uses 0x00).

Edge cases to handle

Empty buffer → 0; single byte; ASCII string "123456789" is the canonical check value (0xF4).

Complexity

O(8 * len).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.