A checksum is a error detection method in Data Communication. It is used for errors which may have been introduced during transmission or storage. It is usually applied to an installation file after it is received from the download server.
The actual procedure which yields the checksum, given a data input is called a checksum function or checksum algorithm.
Checksum method can only detect errors but is unable to correct the error.
In this method a checksum is calculated based on the given binary strings which is sent with the data as redundant bits. This data and checksum is received at receiver end and checksum is calculated again, if checksum is 0 it means no error in data received, else there exists some error in the received data.
For the purpose of this program we are finding checksum for 2 binary strings.
Following is the checksum program in C
#include<stdio.h>
#include<string.h>
#include<stdlib.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\n",carry,complement);
}
else
printf("\nWrong input strings");
}
Here is a sample output