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

Tuesday, 12 June 2012

BIT STUFFING


[ 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(num & code)
return 1;
else
return 0;
}
void bitStuff(char str[])
{
int i=0,k=0,counter=0;
char temp[50];
strcpy(temp,str);
while(temp[i]!='\0')
{
str[k++]=temp[i];
if(temp[i]=='1')
counter++;
else
counter=0;

if(counter==5)
{
str[k++]='0';
}
i++;
}
str[k]='\0';
}
void main()
{
int pid,i,no,k=0,num;
char str[50];
system(">pipe");
pid=open("pipe",O_WRONLY);
printf("Enter Number to Send");
scanf("%d",&num);
for(i=14;i>0;i--)
{
str[k++]=getBinary(num,i)+48;
}
str[k]='\0';
printf("\nBEFORE STUFF : %s",str);
bitStuff(str);
printf("\nAFTER STUFF  : %s",str);
write(pid,&str,sizeof(str));
}

[ RECEIVER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
void deStuff(char str[])
{
int i=0,counter=0,k=0;
char temp[50];
strcpy(temp,str);
while(temp[i]!='\0')
{
str[k++]=temp[i];
if(temp[i]=='1')
counter++;
else
counter=0;
if(counter==5)
i++;
i++;
}
str[k]='\0';
}
void main()
{
int pid,i=0;
char str[50],ans[50];
pid=open("pipe",O_RDONLY);
read(pid,&str,sizeof(str));
printf("\nBEFORE DESTUFF : %s",str);
deStuff(str);
printf("\nAFTER DESTUFF : %s",str);
}

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..........");
}