Skip to content

Instantly share code, notes, and snippets.

@jlamothe
Created May 12, 2012 12:47
Show Gist options
  • Save jlamothe/2666368 to your computer and use it in GitHub Desktop.
Save jlamothe/2666368 to your computer and use it in GitHub Desktop.
CRC16 checksum calculator
#include <stdint.h>
#define CRC16 0x8005
uint16_t gen_crc16(const uint8_t *data, uint16_t size)
{
uint16_t out = 0;
int bits_read = 0, bit_flag;
/* Sanity check: */
if(data == NULL)
return 0;
while(size > 0)
{
bit_flag = out >> 15;
/* Get next bit: */
out <<= 1;
out |= (*data >> (7 - bits_read)) & 1;
/* Increment bit counter: */
bits_read++;
if(bits_read > 7)
{
bits_read = 0;
data++;
size--;
}
/* Cycle check: */
if(bit_flag)
out ^= CRC16;
}
return out;
}
@dolmen
Copy link

dolmen commented Mar 8, 2017

This incorrect code was the subject of a question on Stackoverflow: https://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment