Skip to content

Instantly share code, notes, and snippets.

@gangliao
Created July 1, 2018 06:14
Show Gist options
  • Save gangliao/7517db6ee64a9008bd29901bc83f9b74 to your computer and use it in GitHub Desktop.
Save gangliao/7517db6ee64a9008bd29901bc83f9b74 to your computer and use it in GitHub Desktop.
crc
#define POLYNOMIAL 0xD8 /* 11011 followed by 0's */
uint8_t crc_naive(uint8_t const message)
{
uint8_t remainder;
remainder = message;
for (uint8_t bit = 8; bit > 0; --bit)
{
/* If the uppermost bit is a 1... */
if (remainder & 0x80)
{
/* XOR the previous remainder with the divisor. */
remainder ^= POLYNOMIAL;
}
/* Shift the next bit of the message into the remainder. */
remainder = (remainder << 1);
}
/* Return only the relevant bits of the remainder as CRC. */
return (remainder >> 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment