# Program for Bit Stuffing

OUTPUT:

Enter frame length: 10

Enter input frame (0’s & 1’s only):
1 0 1 0 1 1 1 1 1 1

After stuffing the frame is:
1 0 1 0 1 1 1 1 1 0 1

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include<stdio.h> #include<conio.h> #include<string.h> void main() { int a[20],b[30],i,j,k,count,n; clrscr(); printf("Enter frame length:"); scanf("%d",&n); printf("Enter input frame (0's & 1's only):"); for(i=0;i<n;i++) scanf("%d",&a[i]); i=0; count=1; j=0; while(i<n) { if(a[i]==1) { b[j]=a[i]; for(k=i+1;a[k]==1 && k<n && count<5;k++) { j++; b[j]=a[k]; count++; if(count==5) { j++; b[j]=0; } i=k; }} else { b[j]=a[i]; } i++; j++; } printf("After stuffing the frame is:"); for(i=0;i<j;i++) printf("%d",b[i]); getch(); }```

### 17 Responses to “Program for Bit Stuffing”

1. after for loop plz initilize the count to 1.the it will work for any number of bits having
5 continuous 1’s

2. after for loop plz initilize the count to 1.the it will work for any number of bits having
5 continuous 1’s…………

3. in the bit stuffing program put count=0,then it will run correctly,i.e.it will put 0 after 011111

#include
#include

void main()
{
char num[11], i, j, k, cnt=0;
clrscr();
printf(“Enter the sequence of 10 digit binary numbers: “);
for(i=0;i<10;i++)
scanf("%c", &num[i]);
printf("The 10 digit binary number you have entered is: ");
for(i=0;i<10;i++)
printf("%c", num[i]);
printf("\nAfter stuffing: ");
i=0;
while (ii;k–)
num[k]=num[k-1];
num[i]=0;
num[i]=’0′;
}
}
else
{
i++;
cnt=0;
}
}
for(i=0;i<11;i++)
printf("%c", num[i]);
printf("\nAfter destuffing: ");
i=0;
while (i<10)
{
if(num[i]=='1')
{
i++;
cnt++;
if(cnt==5)
{
for(k=i;k<11;k++)
num[k]=num[k+1];
}
}
else
{
i++;
cnt=0;
}
}
for(i=0;i<10;i++)
printf("%d", num[i]-48);
getch();

5. #include
#include
#include
#include
#include
int main()
{
char str1[50],str2[50];
int i=0,j=0,len,count=0;
int open_status,mknod_status;

printf(“Enter The String : “);
scanf(“%s”,str1);
len=strlen(str1);

printf(“Displaying Bit Values :\n”);
for(i=0;i<len;i++)
{
printf("Bit Values For %c : ",str1[i]);
displaybit(str1[i]);
printf("\n");
}

printf("\nReplacing bit :\n");

for(i=0;i<len;i++)
{
for(j=0;j<=7;j++)
{
if((getbit(str1[i],j)==1))
{
count++;
}
if(count==5)
{
insertbit(count+1,str1[j],0);
count=0;
}
//count++;
}
}

printf("Displaying Bit Values After Changing bit :\n");
for(i=0;i<len;i++)
{
printf("Bit Values For %c : ",str1[i]);
displaybit(str1[i]);
printf("\n");
}

str2[0]='~';
for(i=1;i<len;i++)
{
str2[i]=str1[i-1];
}
str2[i]='~';
len=strlen(str2);
for(i=0;i<len;i++)
{
printf("Character Bit : %c \n",str2[i]);
}

printf("\nNew String: %s",str2);

mknod_status=mknod("pipe",S_IFIFO|S_IRWXU,0);

if(mknod_status<0)
{
printf("error in pipe ");

}
open_status=open("pipe",O_WRONLY);
if(open_status<0)
{
printf("error in open ");

}
write(open_status,str2,sizeof(str2));

close(open_status);
return 0;
}

im unable to execute crc code.. there’s problem with remainder function.. so kindly help me in this regard..

7. shubham siddhartha

Try this program.
Works for more than 10 bits and even if there are 5 cosecutive one’s more than once.
#include
#include
#include
void main() {
int i, j,count=0,nl,cnt=0;
char str[100];
clrscr();
printf(“enter the bit string: “);
gets(str);
while(cnt!=strlen(str)) //checks the complete string till the end
{
for (i=0;i<strlen(str);i++) {
count=0;
//the following code search the six ones in given string
for (j=i;j=(i+5);nl–)
{
str[nl]=str[nl-1];
}
str[i+5]=’0′;
i=i+7;
}
}
cnt++;
}
//adding flag bits(i.e. 01111110) at the beggining and end
printf(“01111110%s01111110”,str);
}