0% found this document useful (0 votes)
41 views9 pages

1.C Code For Checksum: #Include #Include

The document contains code for calculating the checksum and cyclic redundancy check (CRC) of binary strings. The checksum code accepts two binary strings as input, calculates their sum digit-by-digit along with a carry, and outputs the checksum as the complement of the final carry. The CRC code accepts the data length, divisor length and their values as input, generates the transmitted code word by appending zeros, and checks for errors in the received code word using cyclic shifts and XOR operations with the divisor.

Uploaded by

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

1.C Code For Checksum: #Include #Include

The document contains code for calculating the checksum and cyclic redundancy check (CRC) of binary strings. The checksum code accepts two binary strings as input, calculates their sum digit-by-digit along with a carry, and outputs the checksum as the complement of the final carry. The CRC code accepts the data length, divisor length and their values as input, generates the transmitted code word by appending zeros, and checks for errors in the received code word using cyclic shifts and XOR operations with the divisor.

Uploaded by

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

1.

C CODE FOR CHECKSUM

#include<stdio.h>
#include<string.h>

int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;

printf("Enter first binary string\n");


scanf("%s",&a);
printf("Enter second binary string\n");
scanf("%s",&b);

if(strlen(a)==strlen(b)){
length = strlen(a);
char carry='0';

for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';

}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';

}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';

}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';

}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';

}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';

}
else
break;
}

printf("\nSum=%c%s",carry,sum);

for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}

if(carry=='1')
carry='0';
else
carry='1';
printf("\nChecksum=%c%s",carry,complement);
}
else {
printf("\nWrong input strings");
}
}

OUTPUT:
2.CODE FOR CYCLIC REDUNDANCY CHECK

CODE:

#include<stdio.h>
char data[20],div[20],temp[4],total[100];
int i,j,datalen,divlen,len,flag=1;
void check();
int main()
{
printf("Enter the total bit of data:");
scanf("%d",&datalen);
printf("\nEnter the total bit of divisor");
scanf("%d",&divlen);
len=datalen+divlen-1;
printf("\nEnter the data:");
scanf("%s",&data);
printf("\nEnter the divisor");
scanf("%s",div);

for(i=0;i<datalen;i++)
{
total[i]=data[i];
temp[i]=data[i];
}
for(i=datalen;i<len;i++)
total[i]='0';
check();
for(i=0;i<divlen;i++)
temp[i+datalen]=data[i];
printf("\ntransmitted Code Word:%s",temp);
printf("\n\nEnter the received code word:");
scanf("%s",total);
check();
for(i=0;i<divlen-1;i++)
if(data[i]=='1')
{
flag=0;
break;
}
if(flag==1)
printf("\n NO ERRORS!!");
else
printf("\nreceived code word contains errors...\n");
}
void check()
{
for(j=0;j<divlen;j++)
data[j]=total[j];
while(j<=len)
{
if(data[0]=='1')
for(i = 1;i <divlen ; i++)
data[i] = (( data[i] == div[i])?'0':'1');
for(i=0;i<divlen-1;i++)
data[i]=data[i+1];
data[i]=total[j++];
}
}
OUTPUT:

You might also like