VOOZH about

URL: https://www.geeksforgeeks.org/dsa/modulo-2-binary-division/

⇱ Cyclic Redundancy Check and Modulo-2 Division - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Cyclic Redundancy Check and Modulo-2 Division

Last Updated : 24 May, 2025

Cyclic Redundancy Check or CRC 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. 
There are two primary variables in CRC:

  • n: Number of bits in data to be sent from sender side
  • k: Number of bits in the key obtained from generator polynomial.

Encoded Data Generation from Generator Polynomial (Sender Side)

  • The binary data is first augmented by adding k-1 zeros in the end of the data.
  • Then, modulo - 2 binary division is used to divide binary data by the key and remainder of division is stored.
  • At last the the remainder is appended at the end of the data to form the encoded data which is later sent.

Checking Error in Transmission (Receiver Side)

  • After receiving the data, to check if the data is error free, perform the modulo-2 division again.
  • If the remainder is 0, then there are not errors, otherwise, the data is faulty and contain transmission errors.

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 (k-1) bits, which is used for the next step after 1 extra bit is pulled down to make it k bits long.
  • When there are no bits left to pull down, we have a result. The (k-1) bit remainder which is appended at the sender side.

Examples:

Case 1: No error in transmission

Data = 100100, Generator Polynomial (Key) = x3 + x2 + 1 (1101)

Sender Side

👁 gfg
Generating Remainder

The remainder is 001. Thus the data sent is 100100001.

Receiver Side
Code word received at the receiver side 100100001

👁 rational2
Checking the Remainder

The remainder is 0, hence the data received has no errors.

CRC Implementation - O(n) Time and O(n) Space

Case 2: Error in Transmission

Data = 100100, Generator Polynomial (Key) = x3 + x2 + 1 (1101)

Sender Side👁 sender

The remainder is 001. Thus the data sent is 100100001.

Receiver Side
Let there be an error and code word received at the receiver side 100000001.
👁 receiver n

As the remainder is not 0, hence there is some error detected in the receiver side.

Implementation of Cyclic Redundancy Check

The idea is to firstly generate the encoded data by appending the remainder of modulo - 2 division of data and key in the given data. Then, repeat the same process for the data received, and if the decoded data contains any '1', then there is some error in transmission, otherwise the correct data is received.

Follow the below given step-by-step process:

  • Start by appending k-1 zeroes to data to form a new string str, where k is the length of key.
  • Compute the remainder by calling mod2div with dividend set to str and divisor set to key. In mod2div, first slice dividend to the length of divisor, then repeatedly perform the following:
    • If the first character of the current slice (tmp) is '1', call findXor with divisor and tmp; otherwise, call findXor with a string of pick zeroes and tmp.
    • Append the next bit of dividend to the result and increment pick.
  • The function findXor calculates the XOR of two strings a and b by traversing from index 1 to the end: if corresponding bits are the same, it appends "0" to result, otherwise "1".
  • After processing the entire dividend, mod2div returns the final remainder. Append this remainder to data to form the encoded codeword.
  • At the receiver side, extract the first n bits of code and perform mod2div with key to obtain curXor. Then, while cur is not equal to code.size, if curXor’s length is not equal to key’s length, append the next bit from code; otherwise, update curXor by performing mod2div on it.
  • Finally, if curXor’s length equals key’s length, perform a final mod2div. If curXor contains any '1', the data is incorrect; otherwise, the data is correct.

Below is given the implementation:


Output
Sender Side
Data: 100100
Key: 1101
Encoded Data: 100100001

Receiver Side
Data is correct (No errors detected)

CRC Implementation Using Bit Manipulation - O(n) Time and O(n) Space

The idea is to manipulate the given binary strings by converting them to decimal numbers, and process them. After processing the numbers, convert them back to binary strings.

Follow the below given step-by-step approach:

  • Determine n as key.length() and convert key to decimal using toDec(key) to get gen, and convert data to decimal using toDec(data) to get code.
  • Append n-1 zeroes to data by left-shifting code by (n - 1) and store the result in dividend.
  • Compute shft as ceill(log2l(dividend + 1)) - n, which represents the number of least significant bits not involved in the XOR operation.
  • While (dividend >= gen) or (shft >= 0):
    • Calculate rem as (dividend >> shft) XOR gen.
    • Update dividend by combining the unchanged lower shft bits (dividend & ((1 << shft) - 1)) with the new bits (rem << shft) resulting from the XOR operation.
    • Recalculate shft as ceil(log2l(dividend + 1)) - n.
  • After the loop, compute codeword as the bitwise OR of (code << (n - 1)) and dividend.
  • Finally, output the remainder (obtained by converting dividend to binary using toBin(dividend)) and the codeword (obtained by converting codeword to binary using toBin(codeword)).

Output
Remainder: 1
Codeword : 100100001
Comment
Article Tags: