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

CRC and Checksum

The document describes a C program to calculate CRC bits. It includes the code to: 1) Accept data and divisor bits as input, append zeros to make them equal length 2) Exclusively OR corresponding bits of the data and divisor at each step to get the CRC bits 3) Print the calculated CRC bits as output The code is tested on sample data and the output is shown.

Uploaded by

Shashwat Negi
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)
214 views9 pages

CRC and Checksum

The document describes a C program to calculate CRC bits. It includes the code to: 1) Accept data and divisor bits as input, append zeros to make them equal length 2) Exclusively OR corresponding bits of the data and divisor at each step to get the CRC bits 3) Print the calculated CRC bits as output The code is tested on sample data and the output is shown.

Uploaded by

Shashwat Negi
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

17BCE0597 SHASHWAT NEGI

EXERCISE-2

Aim :- To get CRC bits using C program

Code :-
#include<stdio.h>

main()

int da[20],di[20],te[20],tem[20],l;

int i,j,m,n,data,div,t,k,e;

printf("\nEnter the total bit of data and divisor\n");

scanf("%d %d",&data,&div);

m=data+div-1;

printf("\nEnter the data:");

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

{ scanf("%d",&da[i]);te[i]=da[i]; }

for(i=data;i<m;i++)

{ te[i]=0; }

printf("\nEnter the divisor");

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

{ scanf("%d",&di[i]); }

l=div;t=0;

k=0;

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

e=0;t=0;

for(j=1;j<div;j++)
17BCE0597 SHASHWAT NEGI

if(((da[j]==1)&&(di[j]==1))||((da[j]==0)&&(di[j]==0)))

tem[j-1]=0;

if(e!=1)

k=k+1;

t=t+1;

i=i+1;

else

tem[j-1]=1;

e=1;

j=0;

for(e=t;e<div-1;e++)

da[j]=tem[e];

j++;

for(j=j;j<div;j++)

if(l>=data+1)
17BCE0597 SHASHWAT NEGI

da[j]=0;

else

da[j]=te[l];

l=l+1;

printf("\n The CRC BITS are\t ");

for(i=0;i<div-1;i++)

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

}
17BCE0597 SHASHWAT NEGI

Screenshot :-

TERMINAL

CODEBLOCKS
17BCE0597 SHASHWAT NEGI

CHECKSUM CODE IN C

Aim :- To get Checksum bits using C program

CODE-

#include<stdio.h>

#include<math.h>

int sender(int b[10],int k)

int checksum,sum=0,i;

printf("\n****SENDER****\n");

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

sum+=b[i];

printf("SUM IS: %d",sum);

checksum=~sum;

printf("\nSENDER's CHECKSUM IS:%d",checksum);

return checksum;

int receiver(int c[10],int k,int scheck)

int checksum,sum=0,i;

printf("\n\n****RECEIVER****\n");

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

sum+=c[i];

printf(" RECEIVER SUM IS:%d",sum);

sum=sum+scheck;

checksum=~sum;
17BCE0597 SHASHWAT NEGI

printf("\nRECEIVER's CHECKSUM IS:%d",checksum);

return checksum;

}
main()

int a[10],i,m,scheck,rcheck;

printf("\nENTER SIZE OF THE STRING:");

scanf("%d",&m);

printf("\nENTER THE ELEMENTS OF THE ARRAY:");

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

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

scheck=sender(a,m);

rcheck=receiver(a,m,scheck);

if(rcheck==0)

printf("\n\nNO ERROR IN TRANSMISSION\n\n");

else

printf("\n\nERROR DETECTED");
}

OUTPUT-
17BCE0597 SHASHWAT NEGI

BINARY CHECKSUM CALCULATION

Code-
#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';
17BCE0597 SHASHWAT NEGI

}
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");
}
}
17BCE0597 SHASHWAT NEGI

Output-

You might also like