Javatpoint Logo
Javatpoint Logo

CRC program in C

There is a chance that noise during the data transmission process will alter the digital signals carrying the data from the sender to the receiver. Data supplied by the sender may not match data received by the recipient as a result. An error is what we term a change in a data bit, and to avoid one, the data received by the receiver is examined using various error detection techniques. The Cyclic Redundancy Check algorithm is one such example.


  • The Cyclic Redundancy Check algorithm's guiding concepts are explained in this article with examples.
  • Provides two distinct approaches for implementing the CRC program.
  • Explains the two distinct ways to build the CRC program in C and their respective algorithms and time complexity.


The Cyclic Redundancy Check algorithm checks for errors and verifies the accuracy of the data delivered by the sender. CRC requires a generator polynomial in order to compute the check value using binary division in addition to the data that has to be transferred. To ensure that the data is genuine, the check value or CRC is sent with it to the recipient.

The degree of the polynomial can be used as the bit locations to represent the data that will be conveyed to the recipient in polynomial form. The binary data 1010101 of length 7 can be, for instance, represented as,


As the value of that representation is also 0, the bit value of 0 is not represented.

Binary data can also be used to represent the generating polynomial. The degree of the data polynomial must be less than the degree of the generator polynomial and must be larger than 0. Based on the degree of the generating polynomial, the CRC may be divided into several standards. Using a generator polynomial of degree 8 for the CRC-8 standard and degree 16 for the CRC-16 standard. This is a straightforward illustration of the generator polynomial.


It should be noted that Cyclic Redundancy Check may also be employed as a hashing function. Nevertheless, since CRC-8 can only yield 256(28).

The following are the CRC's steps: the sender's side,

  • The generating polynomial of length l and the data of length n are ready.
  • The data that has to be delivered has (l-1) zeros attached to it.
  • The binary version of the generating polynomial is used as the divisor in binary division, with the data acting as the dividend. The check value is the remainder of the binary division.
  • The check value is added to the end of the data before sending the signal.

Check value, or CRC, is represented mathematically as,

CRC program in C

The n in this case refers to the generator polynomial's number of bits. Towards the receiver,

  • The received data is divided once again using binary operations, with the data serving as the dividend and the generating polynomial's binary counterpart serving as the divisor.
  • The data sent from the sender is error-free if the binary division's residual is zero. If the residual does not equal zero, an error has tainted the signal.

The stages in binary division are identical to those in regular polynomial division and are as follows:

  • Dividend and divisor must be XOR at every stage of division. Just the first n bits of the divisor, where n is the number of bits in the dividend, are used for this operation.
  • Based on the n-bit data, the quotient will either be 1 or 0. The quotient is determined by the final piece of the data. If it is 1, it is 1, and if it is 0, it is 0.
  • After the remainder of the aforementioned action, the bit at position n+1 is extracted from the data and added. This leftover amount serves as the next operation's divisor.
  • Up until all the data bits are utilized in the calculation, the action is repeated.

One of the intriguing characteristics of the Cyclic Redundancy Check is the following procedure: CRC(x^y^z) == crc(x)^ crc(y)^ crc (z).

The XOR function is represented by the symbol ^. If we XOR three pieces of data and then CRC the result, the CRC result will be the same as if we CRC each piece of data separately and then XOR with the results.

NOTE: When both inputs are the same, the XOR operator returns 0, and while in all other circumstances it returns 1.

CRC implementation in C

The CRC program may be put into use in C using one of two techniques. The first approach makes use of a character array, whereas the second approach makes use of bit manipulation strategies.


The CRC program's implementation algorithm is as follows:

  • Get the data and the polynomial generator.
  • Let n represent the generator polynomial's length.
  • Add n-1 zeros after the data.
  • Invoke the CRC function.

The CRC function's algorithm is as follows:

  • Get the data's first n bits.
  • If the first bit is 1, then combine the generator polynomial with the first n bits of the data in a XOR operation.
  • To retain the first bit, move the bits one place.
  • Add a little portion of the data. Continue until all of the data's bits have been inserted.

The following truth table of the XOR operator serves as the foundation for the method employed in the XOR function:

Operand 1 Operand 2 Operand(^)
0 0 0
0 1 1
1 1 0
1 0 1
  • When the first and second bits are compared, the result is 0 if the two bits are the same.
  • If the bits vary, the result is 1, otherwise.

The following is the algorithm used to check for faults on the receiver side:

  • A Cyclic Redundancy Check is performed on the newly received data to identify the remaining data.
  • The criteria that the remainder must not include a 1 and that its length must be smaller than the generator polynomial are iterated.
  • We can be certain that there is no mistake if all the items are iterated; otherwise, there is error.
  • In order to determine the size of a character array, utilize the strlen() method. To use this method, the string.h header must be included.



Enter data to be transmitted: 1001101
Enter the Generating polynomial: 1011
Data padded with n-1 zeros : 1001101000
CRC or Check value is : 101
Final data to be sent : 1001101101
Enter the received data: 1001101101
Data received: 1001101101
No error detected


There is no signal error since the data being transferred and received is identical.

Enter the received data: 1001001101
Data received: 1001001101
Error detected

There is a signal error because the data sent and the data received are different.

The execution time and space requirements of the program are represented by the time complexity and space complexity, respectively. The Big-O notation is used to represent the time and space complexity. Due to the fact that two for loops are inferred at once, the program's time complexity is O(n2).

Due to the single dimension character array used to hold n characters, the programme has an O(n) space complexity.

CRC Implementation using Bit Manipulation

The CRC program is implemented using bit manipulation techniques in this way.


The bit manipulation technique used to implement CRC in C is as follows:

  • Get the data and the polynomial generator.
  • Decimalize the generating polynomial and the input data.
  • To append zeros to the end of the data, shift the bits in the data by n-1 places, where n is the length of the generating polynomial.
  • With the help of the logarithmic function, determine how many bits need to be moved.
  • Shift the data by the calculated number of bits and use a generator polynomial to conduct an XOR operation.
  • Identify the remaining part of the procedure, and then add some information to it so that it may be processed further.
  • Continue until every data bit has been used in the calculation.

The algorithm used to translate numbers from decimal to binary is as follows:

  • Use the number and 2 in a modulo operation to determine the residual.
  • Determine the value of 10 raised to the power of the remaining number, and then multiply it by that.
  • Add up the value that was discovered in the previous step.
  • Increase the counter after dividing the decimal by two. Continue doing so until the decimal is no longer equal to 0.


Enter the data to be transmitted: 1001101
Enter the generator polynomial: 1011
Check value or CRC: 101
Data to be sent:  1001101101
Process executed in 1.11 seconds.


The execution time and space requirements of the program are represented by the time complexity and space complexity, respectively. The Big-O notation is used to represent the program's complexity. The program's time complexity will be O(n) since only one for loop is assumed at a time, and its space complexity will also be O(n) because characters are stored in a single-dimensional character array.


  • An error validation procedure called Cyclic Redundancy Check is used to determine whether the signal being sent by the sender has any errors.
  • The data's CRC value is calculated using a generator polynomial, and both the data and CRC are communicated.
  • At every stage of the procedure, the XOR operation is used to determine the CRC value.
  • The receiver side validates itself by determining whether the CRC computed there equals zero.
  • Both the character array and the bit manipulation approach may be used to create the CRC program in C.
  • Both methods have the same level of temporal and spatial complexity.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA