You are on page 1of 20

NAME: AASHISH

REG NO: 21BCE0080

HAMMING CODE:

#include <iostream>
#include <cmath>
#include <string>
#include <unistd.h>
using namespace std;
class Hamming
{
public:
string message;
int codeword[100], temp[100];
int n, check;
char parity;
Hamming()
{
parity = 'E';
message = "";
n = check = 0;
for (int i = 0; i < 100; i++)
{
temp[i] = codeword[i] = 0;
}
}
int findr()
{
for (int i = 1;; i++)
{
if ((n + i + 1) <= pow(2, i))
return i;
}
}
};
class Transmitter : public Hamming
{
public:
void generate()
{
do
{
cout << "Enter the message to be Transmitted, in
Binary (LSB to MSB): ";
cin >> message;
} while (message.find_first_not_of("01") != string::npos);
n = message.size();
cout << "Enter the Parity Type - Odd(O) / Even(E) : ";
cin >> parity;
for (unsigned int i = 0; i < message.size(); i++)
{
if (message[i] == '1')
{
temp[i + 1] = 1;
}
else
{
temp[i + 1] = 0;
}
}
computeCode();
}
void computeCode()
{
check = findr();
cout << "\nNumber of Parity Bits : " << check << endl;
cout << "Total Number of Bits to be Transmitted : " << (n
+ check) << endl
<< endl;
for (int i = (n + check), j = n; i > 0; i--)
{
if ((i & (i - 1)) != 0)
{
codeword[i] = temp[j--];
}
else
{
codeword[i] = setParity(i);
}
}
cout << "PARITY BITS :\n";
for (int i = 0; i < check; i++)
{
cout << "P" << pow(2, i) << " : " <<
codeword[(int)pow(2, i)] << endl;
}
cout << "\nHamming Encoded Output : " << endl;
for (int i = 1; i <= (n + check); i++)
{
cout << codeword[i];
}
cout << endl;

cout << "No error detected ";


}
int setParity(int x)
{
bool flag = true;
int bit;
if (x == 1)
{
bit = codeword[x + 2];
for (int j = (x + 3); j <= (n + check); j++)
{
if (j % 2)
{
bit ^= codeword[j];
}
}
}
else
{
bit = codeword[x + 1];
for (int i = x; i <= (n + check); i++)
{
if (flag)
{
if ((i == x) || (i == (x + 1)))
{
bit = codeword[x + 1];
}
else
{
bit ^= codeword[i];
}
}
if (((i + 1) % x) == 0)
{
flag = !flag;
}
}
}
if (parity == 'O' || parity == 'o')
{
return (!(bit));
}
else
{
return (bit);
}
}
};
class Receiver : public Hamming
{
public:
void correct()
{
do
{
cout << "Enter the Received Data (LSB to MSB) : ";
cin >> message;
} while (message.find_first_not_of("01") != string::npos);
n = message.size();
cout << "Enter the Parity Type - Odd(O) / Even(E) : ";
cin >> parity;
for (unsigned int i = 0; i < message.size(); i++)
{
if (message[i] == '1')
{
codeword[i + 1] = 1;
}
else
{
codeword[i + 1] = 0;
}
}
detect();
}
void detect()
{
int position = 0;
check = findr();
for (int i = 0; i < (n - check); i++)
{
bool flag = true;
int x = pow(2, i);
int bit = codeword[x];
if (x == 1)
{
for (int j = (x + 1); j <= (n + check); j++)
{
if (j % 2)
{
bit ^= codeword[j];
}
}
}
else
{
for (int k = (x + 1); k <= (n + check); k++)
{
if (flag)
{
bit ^= codeword[k];
}
if ((k + 1) % x == 0)
{
flag = !flag;
}
}
}
if ((parity == 'E' || parity == 'e') && (bit == 1))
{
position += x;
}
if ((parity == 'O' || parity == 'o') && (bit == 0))
{
position += x;
}
}
cout << "\nPARITY BITS :\n";
for (int i = 0; i < check; i++)
{
cout << "P" << pow(2, i) << " : " <<
codeword[(int)pow(2, i)] << endl;
}
cout << endl;
if (position != 0)
{
cout << "\nError Detected in the Transmitted Data at
the Position : " << position << endl;
codeword[position] = !codeword[position];
cout << "\nReceived Data Sequence After Correction :
" << endl;
for (int i = 1; i <= (n); i++)
{
cout << codeword[i];
}
cout << endl;
}
else
{
cout << "\nNo Error in the Transmitted Data." << endl;
}
cout << "\nHamming Decoded Output : ";
for (int i = 1; i <= (n); i++)
{
if ((i & (i - 1)) != 0)
{
cout << codeword[i];
}
}
cout << endl;
}
};
int main()
{
string choice;
Transmitter t;
Receiver r;
cout << "\nAre you the Transmitter or the Receiver ? ";
cin >> choice;
if ((choice == "transmitter") || (choice == "Transmitter") ||
(choice == "t") || (choice == "T"))
{
cout << endl;
t.generate();
}
if ((choice == "receiver") || (choice == "Receiver") ||
(choice == "r") || (choice == "R"))
{
cout << endl;
r.correct();
}
return 0;
}
OUTPUT:

CRC:

#include <stdio.h>
#include <string.h>
void crcEncoder(char send_data[], char generator[], int n, int
k)
{
int i, j;
char encoded_data[100];

strcpy(encoded_data, send_data);

for (i = 0; i <= n - k; i++)


{
if (encoded_data[i] == '1')
{
for (j = 0; j < k; j++)
{
encoded_data[i + j] = (encoded_data[i + j] ==
generator[j]) ? '0' : '1';
}
}
}

strcpy(send_data + n, encoded_data + n - k + 1);


}

int crcDecoder(char received_data[], char generator[], int n,


int k)
{
int i, j;
char temp[100];
char rem[100];
strcpy(temp, received_data);
for (i = 0; i < k - 1; i++)
{
temp[n + i] = '0';
}

for (i = 0; i <= n; i++)


{
if (temp[i] == '1')
{
for (j = 0; j < k; j++)
{
temp[i + j] = (temp[i + j] == generator[j]) ? '0' : '1';
}
}
}

strncpy(rem, temp + n, k - 1);


rem[k - 1] = '\0';

return atoi(rem);
}

int isBinaryString(const char *str)


{
int length = strlen(str);

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


{
if (str[i] != '0' && str[i] != '1')
{
return 0; // Not a binary string
}
}

return 1; // Binary string


}

int main()
{
int n, k;
char send_data[100], received_data[100], generator[100];

printf("Enter the number of bits: ");


scanf("%d", &n);

printf("Enter the send data: ");


scanf("%s", send_data);
if (isBinaryString(send_data) == 0)
{
printf("INVALID INPUT.......!");
return 0;
}

printf("Enter the received data: ");


scanf("%s", received_data);
if (isBinaryString(received_data) == 0)
{
printf("INVALID INPUT.......!");
return 0;
}
printf("Enter the generator polynomial: ");
scanf("%s", generator);
if (isBinaryString(generator) == 0)
{
printf("INVALID INPUT.......!");
return 0;
}

k = strlen(generator);

crcEncoder(send_data, generator, n, k);

int error_detected = crcDecoder(received_data, generator,


n, k);

printf("Codeword at Sender Side: %s\n", send_data);


printf("Codeword at Receiver Side: %s\n", received_data);

if (error_detected)
{
printf("Error Detected\n");
}
else
{
printf("No Error Detected\n");
}

return 0;
}
OUTPUT:

CHECKSUM:

#include<stdio.h>
int add(int, int);
int com(int);
void main()
{
int i, j, dl, dil;
int data1[10], data2[10], newdata[10], comp[10],
checksum[10];
printf("\n Enter the data length=");
scanf("%d", &dl);
printf("\n Enter the data1 : \n");
for (i = 0; i < dl; i++)
scanf("%d", &data1[i]);
printf("\n Enter the data2 : \n");
for (i = 0; i < dl; i++)
scanf("%d", &data2[i]);
for (i = dl - 1; i >= 0; i--)
{
newdata[i] = add(data1[i], data2[i]);
}
printf("\n\n Data 1 : ");
for (i = 0; i < dl; i++)
printf("%d", data1[i]);
printf("\n Data 2 : ");
for (i = 0; i < dl; i++)
printf("%d", data2[i]);
printf("\n\n The new data is : ");
for (i = 0; i < dl; i++)
{
printf("%d", newdata[i]);
}
printf("\n Checksum : ");
for (i = 0; i < dl; i++)
{
checksum[i] = com(newdata[i]);
printf("%d", checksum[i]);
}
printf("\n\n Receiver Side : \n");
printf("\n Data : ");
for (i = 0; i < dl; i++)
printf("%d", data1[i]);
printf(" ");
for (i = 0; i < dl; i++)
printf("%d", data2[i]);
printf(" ");
for (i = 0; i < dl; i++)
printf("%d", checksum[i]);
printf("\n Addition : ");
for (i = dl - 1; i >= 0; i--)
{
newdata[i] = add(newdata[i], checksum[i]);
}
for (i = 0; i < dl; i++)
{
printf("%d", newdata[i]);
}
printf("\n Compliment : ");
for (i = 0; i < dl; i++)
{
comp[i] = com(newdata[i]);
printf("%d", comp[i]);
}
}
int add(int x, int y)
{
static int carry = 0;
if (x == 1 && y == 1 && carry == 0)
{
carry = 1;
return 0;
}
else if (x == 1 && y == 1 && carry == 1)
{
carry = 1;
return 1;
}
else if (x == 1 && y == 0 && carry == 0)
{
carry = 0;
return 1;
}
else if (x == 1 && y == 0 && carry == 1)
{
carry = 1;
return 0;
}
else if (x == 0 && y == 1 && carry == 0)
{
carry = 0;
return 1;
}
else if (x == 0 && y == 1 && carry == 1)
{
carry = 1;
return 0;
}
else if (x == 0 && y == 0 && carry == 0)
{
carry = 0;
return 0;
}
else
{
carry = 0;
return 1;
}
}
int com(int a)
{
if (a == 0)
return 1;
else
return 0;
}
OUTPUT:

You might also like