Welcome to the World of Programming

Welcome to the World of Programming

Search this blog

SINGLE BIT STUFFING

/*****************************************************************
This program adds an zero to
a required position given by user (i.e Zero-bit insertion)
******************************************************************/

/* SINGLE BIT STUFFING */

#include<stdio.h>
#define arr 15

int main(){

// variable declaration

int i,n,m,count=1,j;
int a[arr],b[arr];

// user input

printf("Enter No of Bits : ");
scanf("%d",&n);
printf("Enter Number(I/P) in binary : ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

// Single bit stuffing
printf("\nEnter a position where U want to add a ZERO : ");
scanf("%d",&m);
for(i=0,j=0;i<n,j<(n+1);i++,j++){
if(count==m){
b[j]=0; // adding an extra bit '0'
i--;
}
else
b[j]=a[i];
count++;

}

// ERROR checking
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
{
printf("Error position = %d\n",i+1);
break;
}
}

// Displaying output
printf("\n\nThe resultant no(O/P) is : ");
for(j=0;j<(n+1);j++){
printf("%d",b[j]);
}
return 0;

}

No comments:

Post a Comment