0% found this document useful (0 votes)
22 views

Hamming Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Hamming Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

COMPUTER NETWORKS LAB

ASSESSMENT - 1

NAME – SOHAM BANDYOPADHYAY


REG NO. – 21BCE3286
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

CODE

#include <stdio.h>

#include <string.h>

#define MAX_LEN 100

char data[MAX_LEN];

char gen_poly[MAX_LEN];

char check_value[MAX_LEN];

int data_length, gen_poly_length, i, j;

void XOR()

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

if (check_value[i] == gen_poly[i])

check_value[i] = '0';
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

else

check_value[i] = '1';

void receiver()

printf("Enter the received data: ");

scanf("%s", data);

crc();

for (i = 0; (i < gen_poly_length - 1) && (check_value[i] != '1'); i++)

if (i < gen_poly_length - 1)

printf("\nError detected\n\n");

else

printf("\nNo error detected\n\n");

}
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

void crc()

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

check_value[i] = data[i];

do

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

XOR();

for (j = 0; j < gen_poly_length - 1; j++)

check_value[j] = check_value[j + 1];

check_value[j] = data[i++];

} while (i <= data_length + gen_poly_length - 1);

int main()

printf("NAME - SOHAM BANDYOPADHYAY\n");

printf("REG NO. - 21BCE2386\n");

printf("Enter data to be transmitted: ");

scanf("%s", data);

printf("Enter the generator polynomial: ");

scanf("%s", gen_poly);
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

data_length = strlen(data);

gen_poly_length = strlen(gen_poly);

for (int i = data_length; i < data_length + gen_poly_length - 1; i++)

data[i] = '0';

crc();

for (int i = data_length; i < data_length + gen_poly_length - 1; i++)

data[i] = check_value[i - data_length];

printf("Final data to be sent: %s\n", data);

printf("CRC is: %s\n", check_value);

receiver();

return 0;

OUTPUT SCREENSHOT

ERROR NOT DETECTED

ERROR DETECTED
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

HAMMING CODE
CODE
#include <stdio.h>

#include <math.h>

int input[32];

int code[32];

int ham_calc(int position, int c_l)

int count = 0, i, j;

i = position - 1;

while (i < c_l)

for (j = i; j < i + position; j++)

if (code[j] == 1)

count++;

i = i + 2 * position;

if (count % 2 == 0)

return 0;

else

return 1;

int main()

int n, i, p_n = 0, c_l, j, k;

printf("\nName - Soham Bandyopadhyay");

printf("\nReg No. - 21BCE2386");


NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

printf("\nEnter length of the Data Word: ");

scanf("%d", &n);

printf("Enter the Data Word:\n");

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

scanf("%d", &input[i]);

i = 0;

while (n > (int)pow(2, i) - (i + 1))

p_n++;

i++;

c_l = p_n + n;

j = k = 0;

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

if (i == ((int)pow(2, k) - 1))

code[i] = 0;

k++;

else

code[i] = input[j];

j++;

}
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

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

int position = (int)pow(2, i);

int value = ham_calc(position, c_l);

code[position - 1] = value;

printf("The Code Word is: ");

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

printf("%d", code[i]);

printf("\n");

printf("Enter received Code Word:\n");

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

scanf("%d", &code[i]);

int error_pos = 0;

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

int position = (int)pow(2, i);

int value = ham_calc(position, c_l);

if (value != 0)

error_pos += position;

if (error_pos+1 == 1)

printf("The received Code Word is correct.\n");

else

printf("Error at bit position: %d\n", error_pos);

return 0;

SCREENSHOT
NAME – SOHAM BANDYOAPDHYAY
REG NO. – 21BCE2386

Error Detected

Error Not Detected

You might also like