Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@dolmen 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
You can’t perform that action at this time.