fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. uint8_t crc2(uint8_t data) {
  5. uint8_t poly = 0x07; // CRC-2 polynomial (111)
  6. uint8_t crc = 0x00;
  7.  
  8. for (int i = 0; i < 8; i++) {
  9. uint8_t bit = ((data >> (7 - i)) & 1) ^ ((crc >> 1) & 1);
  10. crc = (crc << 1) & 0x03; // keep only 2 bits
  11.  
  12. if (bit)
  13. crc ^= poly & 0x03; // apply CRC-2 polynomial
  14. }
  15. return crc & 0x03;
  16. }
  17.  
  18. int main() {
  19. uint8_t data = 0xAB;
  20. uint8_t result = crc2(data);
  21.  
  22. printf("CRC-2 of 0x%X = %u\n", data, result);
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
CRC-2 of 0xAB = 2