DevTools Hub

Guides › What Is CRC? How CRC-16 MODBUS, CCITT and CRC-32 Work

What Is CRC? How CRC-16 MODBUS, CCITT and CRC-32 Work

CRC (Cyclic Redundancy Check) is an error-detecting code that tells you whether data was corrupted in transit or storage. The sender appends a CRC value to the data; the receiver computes it again, and if the two values differ the data is treated as damaged. Modbus RTU, CAN, USB, Ethernet, ZIP and PNG all rely on CRCs.

Why CRC is needed

Industrial cabling such as RS-485 runs next to motors, drives and welders, so electrical noise flipping a bit is common. If one bit of the temperature value 0x00FA (25.0 °C) flips to give 0x80FA, the value is completely wrong. With a CRC the receiver discards the frame and asks again instead of acting on bad data.

CRC vs simple checksums

MethodHow it worksWeakness
Checksum (sum)Adds all bytesMisses reordered bytes, or one byte +1 and another −1
LRC (Modbus ASCII)Two's complement of the byte sumSimilar limits
CRCRemainder of a polynomial divisionVery strong against random errors, but not against deliberate tampering
Hash (SHA-256)Cryptographic hashToo heavy for most microcontroller links

A CRC always detects: every single-bit error, every double-bit error within a bounded frame length, any odd number of bit errors (when the polynomial has a factor of (x+1)), and any burst error no longer than the CRC width. A CRC is not a security feature: anyone who changes the data can simply recompute and append a new CRC.

How it works: binary polynomial division

A CRC treats the data bits as one large binary polynomial and divides it by a fixed generator polynomial; the remainder is the CRC. Addition and subtraction are XOR without carries. The common CRC-16 polynomial x16 + x15 + x2 + 1, written without its top term, is 0x8005.

  1. Load the CRC register with the initial value (Init).
  2. XOR the next data byte into the register.
  3. Shift one bit at a time; whenever the bit shifted out is 1, XOR the polynomial in. Repeat 8 times.
  4. After the last byte, apply the final XOR (XorOut). The result is the CRC.

Reading CRC parameters

"CRC-16" alone does not define a result. Two 16-bit CRCs with different parameters give completely different values, so check these in the device manual first.

ParameterMeaning
WidthNumber of CRC bits (8, 16, 32...)
PolyGenerator polynomial (top term omitted)
InitRegister value before processing
RefInWhether each input byte is bit-reflected (LSB first)
RefOutWhether the final result is bit-reflected
XorOutValue XORed into the final result
CheckResult for the ASCII string "123456789"; use it to verify an implementation

Common CRC variants

NamePolyInitRefIn/OutXorOutCheckUsed in
CRC-16/MODBUS0x80050xFFFFTrue/True0x00000x4B37Modbus RTU
CRC-16/CCITT-FALSE (IBM-3740)0x10210xFFFFFalse/False0x00000x29B1Serial protocols, firmware images
CRC-16/XMODEM0x10210x0000False/False0x00000x31C3XMODEM file transfer
CRC-16/KERMIT0x10210x0000True/True0x00000x2189KERMIT, some Bluetooth
CRC-32 (ISO-HDLC)0x04C11DB70xFFFFFFFFTrue/True0xFFFFFFFF0xCBF43926Ethernet, ZIP, PNG, gzip

"CCITT" is used loosely for several variants with different Init values. If a manual only says CCITT, use the check values above to find out which one it means. This site's calculator supports MODBUS, CCITT-FALSE and CRC-32.

CRC-16/MODBUS example

The request 01 03 00 00 00 0A (read 10 holding registers from slave 1) has CRC 0xCDC5. Modbus RTU sends the CRC low byte first, so the frame on the wire is:

01 03 00 00 00 0A C5 CD

Enter 01 03 00 00 00 0A in HEX mode in the calculator to confirm. Appending the two CRC bytes in the wrong order is the most common reason a hand-built frame gets no reply.

C code (bitwise)

uint16_t crc16_modbus(const uint8_t *buf, size_t len) {
    uint16_t crc = 0xFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= buf[i];
        for (int b = 0; b < 8; b++)
            crc = (crc & 1) ? (crc >> 1) ^ 0xA001 : crc >> 1;
    }
    return crc;   /* send crc & 0xFF first, then crc >> 8 */
}

0xA001 is 0x8005 bit-reversed. For reflected CRCs (RefIn/RefOut true), shifting right with the reversed polynomial avoids reflecting every byte.

CRC-16/CCITT-FALSE

uint16_t crc16_ccitt_false(const uint8_t *buf, size_t len) {
    uint16_t crc = 0xFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= (uint16_t)buf[i] << 8;
        for (int b = 0; b < 8; b++)
            crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : crc << 1;
    }
    return crc;
}

Faster: table-driven CRC

The bitwise version loops 8 times per byte. When throughput matters, precompute a 256-entry table (512 bytes for a 16-bit CRC) and do one lookup per byte. Use the bitwise version on very small MCUs and the table version when memory allows.

ASCII input vs HEX input

Entering 01 03 as ASCII computes the CRC over the characters 0, 1, space, 0, 3 (bytes 30 31 20 30 33). In HEX mode it uses the bytes 01 03. Use HEX mode to verify communication frames.

Common mistakes

The full Modbus frame layout is covered in the Modbus guide.

Try it now: CRC Calculator (runs in your browser, no data is sent anywhere)