2

I understand how CRC for a byte can be calculated by modulo 2 division with a polynomial but I don't understand how do you calculate CRC for data consisting of byte arrays. CRC for a single byte can be calculated by this following code

#define WIDTH  8
#define TOPBIT 1 << 7
#define POLYNOMIAL 0xD8

uint8_t(uint8_t const message)
{
    uint8_t remainder = 0;  
    remainder ^= message;
    for (uint8_t bit = 8; bit > 0; --bit)
    {
        if (remainder & TOPBIT)
        {
            remainder = (remainder << 1) ^ POLYNOMIAL;
        }
        else
        {
            remainder = (remainder << 1);
        }
    }
    return (remainder);

}

but what about byte array ? I found above code on this site, Author also gave the code for byte array where he just XOR'ed current remainder with next byte

remainder ^= (message[byte] << (WIDTH - 8));

I don't quite understand why? why he XOR'ed to get in the next byte into remainder?

11
  • 1
    Not clear what your problem is. We are not to explain CRC calculation,that is too broad. There are a lot of resources about CRCs to be found. Start with Wikipedia and dig further. Commented Jul 19, 2017 at 13:08
  • 1
    @Olaf care to explain? Commented Jul 19, 2017 at 13:09
  • Wikipedia has an extensive article on CRC and how to calculate them. Commented Jul 19, 2017 at 13:12
  • 2
    Tip: Use () with TOPBIT (1 << 7) to avoid unexpected code - although not an issue here. Commented Jul 19, 2017 at 14:28
  • 1
    The missing parentheses in the TOPBIT define (as @chux wrote above) indicate that you changed the original code from the article. Do not do this if you don't understand what you're doing. Also, if you are not too tight with RAM, you might want to check the lookup table CRC version shown at the bottom of the article. Commented Jul 22, 2017 at 8:03

2 Answers 2

2

See A painless guide to CRC error detection algorithms. It has everything on CRCs, including your question. An array is treated as a single massive number so the remainder is carried over to the next byte. CRC is the remainder that is left over at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

@kiner_shah Alternatively get it here
0

Look at page on wikipedia Mathematics of cyclic redundancy checks It turns out that CRC is a linear operation meaning that crc(x^y^z) = crc(x)crc(y)crc(x) and hence the author XOR'd the remainder of previous byte with the next byte

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.