Search code examples
crc

How to calculate the 6 Bit CRC using the c programming lanuguage?


I'm using Encoder where I'm getting the 0 to 24 bits, from which 0 to 5 bits are CRC bits. Can anyone help me to understand how to calculate the CRC code for those 6 bits?

layout of transmitted bits for three encoder models linear feedback shift register schematic for the CRC


Solution

  • Without an example, I can't be sure, but this will compute the CRC as described, assuming that the initial values of the flip flops are zero.

    unsigned crc6biss(uint32_t data, unsigned bits) {
        while (bits--)
            data = data & 0x80000000 ? (data << 1) ^ 0x0c000000 : data << 1;
        return ~data >> 26;
    }
    

    Here data are your data bits, but, very importantly, shifted up in the 32-bit word so that the most significant bit of the data is the most significant bit of data. bits is how many bits are to be processed. So the data bits are the bits 32-bits..31 of data. The remaining bits are zeros. The 6-bit CRC is returned in the least significant six bits of the return value.

    From your documentation, bits would be 18, 19, or 21.