Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday 12 June 2012

Single Bit Parity

[ SENDER SIDE ]



#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getbinary(int num,int pos)
{
int code=1;
code=code<<(pos-1);
if(code & num)
return 1;
else
return 0;
}
int getparity(char ans[])
{
int i=0,counter=0;
for(i=0;i<14;i++)
{
if(ans[i]=='1')
counter++;
}
if(counter % 2 == 0 )
return 0;
else
return 1;
}
void generateError(char ans[])
{
int exitflag=1,pos;
do
{
printf("Enter Position : ");
scanf("%d",&pos);
if(pos>=0 && pos<14)
{
if(ans[pos]=='1')
ans[pos]='0';
else
ans[pos]='1';
}
else
{
printf(" \t>>>Position is out of range<<<\n");
}
printf("Want to Generate More Error (1 / 0) : ");
scanf("%d",&exitflag);
}while(exitflag==1);
}
void main()
{
int num,i,k=0,flag,pid;
char ans[16];
pid=open("pipe",O_WRONLY);
printf("Enter number : ");
scanf("%d",&num);
for(i=14;i>0;i--)
{
ans[k++]=getbinary(num,i) + 48;
}
ans[k++]=getparity(ans) + 48;
ans[k++]='\0';
printf("\n======================================================");
printf("\nData to Be Send With Parity : %s",ans);
printf("\n======================================================\n");
printf("\nWant to Generate Error (1 / 0) : ");
scanf("%d",&flag);
if(flag==1)
{
generateError(ans);
printf("\n======================================================");
printf("\nData to Be Send With Parity and with Error : %s",ans);
printf("\n======================================================\n");
}
write(pid,&ans,sizeof(ans));
close(pid);
}




[ RECEIVER SIDE ]


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getparity(char ans[])
{
int i=0,counter=0;
for(i=0;i<14;i++)
{
if(ans[i]=='1')
counter++;
}
if(counter % 2 == 0 )
return 0;
else
return 1;
}
void main()
{
int pid,i=0;
char ans[16];
pid=open("pipe",O_RDONLY);
read(pid,&ans,sizeof(ans));
if(ans[14]== (getparity(ans)+48) )
printf("...........NO ERROR.............");
else
printf("...........ERROR DETECTED..........");
}

1 comment: