This is a practical guide to the Cyclic Redundancy Check (CRC). For a deep dive into the theory and math behind CRC, check out my the post Math Behind Cyclic Redundancy Check.
This post might be particularly helpful for those learning about CRC in a computer networking course.
CRC with Example
The data is and the generator is .
Sender and Receiver...
Both have access to the generator polynomial: .
Both have access to the degree of . The degree is simply the number of bits minus 1(starting from the left most nonzero bit).
is a polynomial over , which means it's coefficients are either or .
For example, . So, when we see that is a 4 bit string, we say it is of degree .
Sender
Need to transmit data encoded in a message so that the receiver can use on the message it receives, which may or may not be the same as , to check if there were any bit errors over transmission.
IDEA: Transform the data-polynomial into a message-polynomial that is divisible by the generator-polynomial and if any errors occur (changing the coefficients of the message-polynomial) then the received message-polynomial won't be divisible by (to some extent).
If we use a generator with bits, then we can detect burst errors less than or equal to bits. In our example, this means we can detect up to bit burst errors. This is what I mean by to some extent.
Walkthrough...
.
Now shift times:
.
And now we divide by to calculate the remainder . Note we are working over , so we can simplify this by treating it like bitwise XOR operations.
1 1 0 1 0 1 1 0 0 0
1 0 0 1
----------
0 1 0 0 <- XOR result. Left most is 0, so drop it and carry down next bit.
1 0 0 0 <- leftmost bit is 1. Now XOR with G again.
1 0 0 1 <- and rinse and repeat...
--------
0 0 0 1
0 0 1 1
0 1 1 1
1 1 1 0
1 0 0 1
--------
0 1 1 1
1 1 1 0
1 0 0 1
--------
0 1 1 1
1 1 1 0
1 0 0 1
--------
0 1 1 1
Our remainder's degree will be one less than the degree of the generator, or 1 less bit. Take bits from and we have .
Now craft the message-polynomial as such:
is now divisible by and through some things explained only by mystical abstract algebra stuff, if you alter the coefficients of , it won't be divisible by (to some extent).
Receiver
Simply take your received message, we will call it, and divide by . If , then we detect no errors and it's very likely that .
If then we KNOW that , and thus, there was an error.
If there is no error, you retrieve from through simply stripping off the most bits. In our case, you strip off the most bits.
No Errors
1 1 0 1 0 1 1 1 1 1
1 0 0 1
----------
0 1 0 0
1 0 0 0
1 0 0 1
--------
0 0 0 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
--------
0 1 1 0
1 1 0 1
1 0 0 1
-------
0 1 0 0
1 0 0 1
1 0 0 1
--------
0 0 0 0
. No error detected.
Errors
1 0 1 1 0 1 1 1 1 1
1 0 0 1
--------
0 0 1 0
0 1 0 0
1 0 0 1
1 0 0 1
--------
0 0 0 0
0 0 0 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
--------
0 1 1 0
. Error detected.