cybersecurity · intermediate · ~12 min
Implement a reflected CRC with the bitwise polynomial loop.
#include <stdint.h>
#include <stddef.h>
uint32_t crc32(const uint8_t *data, size_t n);
Compute the standard IEEE CRC-32: init 0xFFFFFFFF, reflected polynomial
0xEDB88320, final XOR 0xFFFFFFFF. Known answers: "" → 0x00000000,
"123456789" → 0xCBF43926.
crc ^= byte, then 8× crc = (crc>>1) ^ (poly & -(crc&1)).~crc.CRC-32 (IEEE) is the integrity check behind zip, png, and ethernet frames. Implementing it nails the reflected-polynomial bit loop.
#include <stdint.h>
#include <stddef.h>
uint32_t crc32(const uint8_t *data, size_t n) {
/* TODO */
(void)data; (void)n;
return 0;
}
Wrong (non-reflected) polynomial. Forgetting init 0xFFFFFFFF or the final ~. Signed-shift surprises.
Empty input → 0. The canonical "123456789" check value.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.