C · 2026-06-16 · embedded · CRC · checksum · communication

CRC-8 in C (compact, no table)

When you just need to catch line errors on a short frame and don’t want to spend 256 bytes of flash on a lookup table, a bitwise CRC-8 is the right tool. This one defaults to polynomial 0x07 (CRC-8/SMBUS), but any 8-bit polynomial drops in.

#include <stdint.h>
#include <stddef.h>

#define POLY 0x07u               /* CRC-8/SMBUS; 0x31 = Dallas/Maxim */

/*******************************************************************************
 * Function Name  : crc8
 * Description    : Table-free bitwise CRC-8 over a byte buffer.
 * Input          : p - pointer to the data; n - number of bytes.
 * Output         : None.
 * Return         : The 8-bit CRC.
 *******************************************************************************/
uint8_t crc8(const uint8_t *p, size_t n)
{
  uint8_t crc = 0;

  while (n--)
  {
    crc ^= *p++;

    for (int i = 0; i < 8; i++)
    {
      crc = (crc & 0x80) ? (crc << 1) ^ POLY : crc << 1;
    }
  }

  return crc;
}

Check value

For the ASCII string "123456789" this returns 0xF4 — the standard CRC-8/SMBUS check value, so you can verify a port against any reference implementation.

Usage

  • Append the returned byte to your frame; the receiver runs the same routine over the payload and compares.
  • Need CRC-16 or CRC-32, reflection, or generated C with a table? Use the interactive CRC calculator to pick parameters and export code.
  • This is for error detection, not security — it won’t stop a deliberate tamper.