CRC or Cyclic Redundancy Check is a method of detecting accidental changes/errors in the communication channel.
CRC uses Generator Polynomial which is available on both sender and receiver side. An example generator polynomial is of the form like x3 + x + 1. This generator polynomial represents key 1011. Another example is x2 + 1 that represents key 101.
n : Number of bits in data to be sent from sender side. k : Number of bits in the key obtained from generator polynomial.
Sender Side (Generation of Encoded Data from Data and Generator Polynomial (or Key)):
- The binary data is first augmented by adding k-1 zeros in the end of the data
- Use modulo-2 binary division to divide binary data by the key and store remainder of division.
- Append the remainder at the end of the data to form the encoded data and send the same
Receiver Side (Check if there are errors introduced in transmission)
Perform modulo-2 division again and if the remainder is 0, then there are no errors.
In this article we will focus only on finding the remainder i.e. check word and the code word.
Modulo 2 Division:
The process of modulo-2 binary division is the same as the familiar division process we use for decimal numbers. Just that instead of subtraction, we use XOR here.
- In each step, a copy of the divisor (or data) is XORed with the k bits of the dividend (or key).
- The result of the XOR operation (remainder) is (n-1) bits, which is used for the next step after 1 extra bit is pulled down to make it n bits long.
- When there are no bits left to pull down, we have a result. The (n-1)-bit remainder which is appended at the sender side.
Example 1 (No error in transmission):
Data word to be sent - 100100 Key - 1101 [ Or generator polynomial x3 + x2 + 1] Sender Side: Therefore, the remainder is 001 and hence the encoded data sent is 100100001. Receiver Side: Code word received at the receiver side 100100001 Therefore, the remainder is all zeros. Hence, the data received has no error.
Example 2: (Error in transmission)
Data word to be sent - 100100 Key - 1101 Sender Side: Therefore, the remainder is 001 and hence the code word sent is 100100001. Receiver Side Let there be an error in transmission media Code word received at the receiver side - 100000001
Below is Python implementation for generating code word from given binary data and key.
('Remainder : ', '001') ('Encoded Data (Data + Remainder) : ', '100100001')
Remainder : 001 Encoded Data (Data + Remainder) : 100100001
Note that CRC is mainly designed and used to protect against common of errors on communication channels and NOT suitable protection against intentional alteration of data (See reasons here)
Implementation using Bit Manipulation:
CRC codeword generation can also be done using bit manipulation methods as follows:
Remainder: 100 Codeword : 10011101100
This article is contributed by Jay Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Cyclic shifts of integer N by another integer m
- Check if N leaves only distinct remainders on division by all values up to K
- Calculate 7n/8 without using division and multiplication operators
- Divide two integers without using multiplication, division and mod operator
- Compute modulus division by a power-of-2-number
- Modular Division
- Fast average of two numbers without division
- First number to leave an odd remainder after repetitive division by 2
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Check if two numbers are equal without using arithmetic and comparison operators
- Check if binary representation of a given number and its complement are anagram
- Check whether the number has only first and last bits set
- Check whether the number has only first and last bits set | Set 2
- Check if bitwise AND of any subset is power of two
- Check if a number has same number of set and unset bits
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check whether bitwise AND of N numbers is Even or Odd
- Check if left and right shift of any string results into given string
- Write an Efficient Method to Check if a Number is Multiple of 3