Created
July 1, 2018 06:14
-
-
Save gangliao/7517db6ee64a9008bd29901bc83f9b74 to your computer and use it in GitHub Desktop.
crc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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