CN Exp3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Name: Bhavik Shah

Computer Networks SAP_Id: 60004200044


Division/Batch: A-2
Experiment 3

Aim: - To implement CRC and hamming code as error detection and correction codes.

Topics: - CRC, Hamming Code

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

where, r = redundant bit, m = data bit

Suppose the number of data bits is 7, then the number of redundant bits can be calculated using:

= 2^4 ≥ 7 + 4 + 1

Thus, the number of redundant bits= 4


Parity bits –A parity bit is a bit appended to a data of binary bits to ensure that the total number of 1’s in the
data is even or odd. Parity bits are used for error detection. There are two types of parity bits:

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>

using namespace std;

string xor1(string a, string b)

string result = "";

int n = b.length();

for(int i = 1; i < n; i++)

if (a[i] == b[i])

result += "0";

else

result += "1";

return result;

string mod2div(string divident, string divisor)

int pick = divisor.length();

string tmp = divident.substr(0, pick);

int n = divident.length();
while (pick < n)

if (tmp[0] == '1')

tmp = xor1(divisor, tmp) + divident[pick];

else

tmp = xor1(std::string(pick, '0'), tmp) +

divident[pick];

pick += 1;

if (tmp[0] == '1')

tmp = xor1(divisor, tmp);

else

tmp = xor1(std::string(pick, '0'), tmp);

return tmp;

void encodeData(string data, string key)

int l_key = key.length();

string appended_data = (data + std::string(l_key - 1, '0'));

string remainder = mod2div(appended_data, key);


string codeword = data + remainder;

cout << "Remainder : "

<< remainder << "\n";

cout << "Encoded Data (Data + Remainder) :"

<< codeword << "\n";

int main()

string data,key;

cout << "Enter Data: ";

cin>>data;

cout << "Enter Key: ";

cin>>key;

encodeData(data, key);

return 0;

Output:

b)decoding
#include <bits/stdc++.h>

using namespace std;

string xor1(string a, string b)

{
string result = "";

int n = b.length();

for (int i = 1; i < n; i++)

if (a[i] == b[i])

result += "0";

else

result += "1";

return result;

string mod2div(string divident, string divisor)

int pick = divisor.length();

string tmp = divident.substr(0, pick);

int n = divident.length();

while (pick < n)

if (tmp[0] == '1')

tmp = xor1(divisor, tmp) + divident[pick];

else

tmp = xor1(std::string(pick, '0'), tmp) +

divident[pick];

pick += 1;
}

if (tmp[0] == '1')

tmp = xor1(divisor, tmp);

else

tmp = xor1(std::string(pick, '0'), tmp);

return tmp;

void encodeData(string data, string key)

int l_key = key.length();

string check = std::string(l_key - 1, '0');

string remainder = mod2div(data, key);

for (int i = data.length()-1; i >data.length()-l_key ; i--) {

data[i]='\0';

if(remainder==check){

cout<<"Input is not Corrupted"<<endl;

cout<<"Data Is: "<<data;

else{

cout<<"Input is Corrupted"<<endl;

int main()

string data, key;

cout << "Enter Data: ";


cin >> data;

cout << "Enter Key: ";

cin >> key;

encodeData(data, key);

return 0;

Output-

2.Hamming Code-
#include <iostream>

#include <math.h>

using namespace std;

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;

for (int i = k; i <= n; i++)

if ((i % (int)pow(2, k + 1)) >= ((int)pow(2, k)))

sum += arr[i - 1];

return sum % 2;

int main()

int n, r;

cout << "Enter Lenght : ";

cin >> n;

r = getNo(n);

int input[n];

int output[n + r];

cout << "Enter Data: ";

int power = r;

int counter = 0;

for (int i = n + r - 1; i >= 0; i--)

if (i + 1 == pow(2, power - 1))

input[i] = 0;

power--;
}

else

cin >> input[i];

for (int i = 0; i < r; i++)

input[((int)pow(2, i)) - 1] = getParity(input, i, n + r);

cout << "Hamming Code: ";

for (int i = n + r - 1; i >= 0; i--)

cout << input[i] << " ";

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++.

You might also like