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

CRC & Hamming Code

crc

Uploaded by

adittyadas813
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CRC & Hamming Code

crc

Uploaded by

adittyadas813
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Write a Program to implement cyclic redundancy check in c

Program :

// Include headers

#include<stdio.h>

#include<string.h>

// length of the generator polynomial

#define N strlen(gen_poly)

// data to be transmitted and received

char data[28];

// CRC value

char check_value[28];

// generator polynomial

char gen_poly[10];

// variables

int data_length,i,j;

// function that performs XOR operation

void XOR(){

// if both bits are the same, the output is 0

// if the bits are different the output is 1

for(j = 1;j < N; j++)

check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');

// Function to check for errors on the receiver side

void receiver(){

// get the received data


printf("Enter the received data: ");

scanf("%s", data);

printf("\n-----------------------------\n");

printf("Data received: %s", data);

// Cyclic Redundancy Check

crc();

// Check if the remainder is zero to find the error

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

if(i<N-1)

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

else

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

void crc(){

// initializing check_value

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

check_value[i]=data[i];

do{

// check if the first bit is 1 and calls XOR function

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

XOR();

// Move the bits by 1 position for the next computation

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

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

// appending a bit from data


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

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

// loop until the data ends

int main()

// get the data to be transmitted

printf("\nEnter data to be transmitted: ");

scanf("%s",data);

printf("\n Enter the Generating polynomial: ");

// get the generator polynomial

scanf("%s",gen_poly);

// find the length of data

data_length=strlen(data);

// appending n-1 zeros to the data

for(i=data_length;i<data_length+N-1;i++)

data[i]='0';

printf("\n----------------------------------------");

// print the data with padded zeros

printf("\n Data padded with n-1 zeros : %s",data);

printf("\n----------------------------------------");

// Cyclic Redundancy Check

crc();

// print the computed check value

printf("\nCRC or Check value is : %s",check_value);


// Append data with check_value(CRC)

for(i=data_length;i<data_length+N-1;i++)

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

printf("\n----------------------------------------");

// printing the final data to be sent

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

printf("\n----------------------------------------\n");

// Calling the receiver function to check errors

receiver();

return 0;

Output :

In case of error :
Write a program to implement Hamming code in C

Program:

#include<stdio.h>

void main() {

int data[10];

int dataatrec[10],c,c1,c2,c3,i;

printf("Enter 4 bits of data one by one\n");

scanf("%d",&data[0]);

scanf("%d",&data[1]);

scanf("%d",&data[2]);

scanf("%d",&data[4]);

//Calculation of even parity

data[6]=data[0]^data[2]^data[4];

data[5]=data[0]^data[1]^data[4];

data[3]=data[0]^data[1]^data[2];

printf("\nEncoded data is\n");

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

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

printf("\n\nEnter received data bits one by one\n");


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

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

c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];

c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];

c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];

c=c3*4+c2*2+c1 ;

if(c==0) {

printf("\nNo error while transmission of data\n");

else {

printf("\nError on position %d",c);

printf("\nData sent : ");

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

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

printf("\nData received : ");

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

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

printf("\nCorrect message is\n");

//if errorneous bit is 0 we complement it else vice versa

if(dataatrec[7-c]==0)
dataatrec[7-c]=1;

else

dataatrec[7-c]=0;

for (i=0;i<7;i++) {

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

Output :

You might also like