CN Exp3
CN Exp3
CN Exp3
Aim: - To implement CRC and hamming code as error detection and correction codes.
Introduction: -
CRC or Cyclic Redundancy Check is a method of detecting accidental changes/errors in the communication
channel.
In CRC technique, a string of n 0s is appended to the data unit, and this n number is less than the
number of bits in a predetermined number, known as division which is n+1 bits.
Secondly, the newly extended data is divided by a divisor using a process is known as binary division.
The remainder generated from this division is known as CRC remainder.
Thirdly, the CRC remainder replaces the appended 0s at the end of the original data. This newly
generated unit is sent to the receiver.
The receiver receives the data followed by the CRC remainder. The receiver will treat this whole unit
as a single unit, and it is divided by the same divisor that was used to find the CRC remainder.
If the resultant of this division is zero which means that it has no error, and the data is accepted.
If the resultant of this division is not zero which means that the data consists of an error. Therefore, the data
is discarded.
Hamming code is a set of error-correction codes that can be used to detect and correct the errors that can
occur when the data is moved or stored from the sender to the receiver. It is technique developed by R.W.
Hamming for error correction.
Redundant bits –Redundant bits are extra binary bits that are generated and added to the information-
carrying bits of data transfer to ensure that no bits were lost during the data transfer.
The number of redundant bits can be calculated using the following formula:
2^r ≥ m + r + 1
Suppose the number of data bits is 7, then the number of redundant bits can be calculated using:
= 2^4 ≥ 7 + 4 + 1
Even parity bit: In the case of even parity, for a given set of bits, the number of 1’s are counted. If that count
is odd, the parity bit value is set to 1, making the total count of occurrences of 1’s an even number. If the
total number of 1’s in a given set of bits is already even, the parity bit’s value is 0.
1.CRC-
a)Encoding
#include<bits/stdc++.h>
int n = b.length();
if (a[i] == b[i])
result += "0";
else
result += "1";
return result;
int n = divident.length();
while (pick < n)
if (tmp[0] == '1')
else
divident[pick];
pick += 1;
if (tmp[0] == '1')
else
return tmp;
int main()
string data,key;
cin>>data;
cin>>key;
encodeData(data, key);
return 0;
Output:
b)decoding
#include <bits/stdc++.h>
{
string result = "";
int n = b.length();
if (a[i] == b[i])
result += "0";
else
result += "1";
return result;
int n = divident.length();
if (tmp[0] == '1')
else
divident[pick];
pick += 1;
}
if (tmp[0] == '1')
else
return tmp;
data[i]='\0';
if(remainder==check){
else{
cout<<"Input is Corrupted"<<endl;
int main()
encodeData(data, key);
return 0;
Output-
2.Hamming Code-
#include <iostream>
#include <math.h>
int getNo(int m)
int i = 0;
while (1)
if (pow(2, i) >= m + i + 1)
return i;
i++;
}
int getParity(int arr[], int k, int n)
int sum = 0;
return sum % 2;
int main()
int n, r;
cin >> n;
r = getNo(n);
int input[n];
int power = r;
int counter = 0;
input[i] = 0;
power--;
}
else
return 0;
Output-
Conclusion: - In this experiment, we learnt about CRC and Hamming code as error detection and
correction mechanism, saw their implementation and coded them in C++.