This post contains the implementation of Bit Stuffing in C programming. Learn about bit-stuffing and with source code for Bit Stuffing in c.
What is Bit Stuffing?
Bit Stuffing is the technique of insertion of one or more extra bits (0) into data. These are the non–information bits as a way to provide signaling information to a receiver.
On the receiver end, the stuffed 0 bit is removed and the original value is retrieved.
This bit (0) is added after every 5 consecutive 1’s bit frame sequence as shown in a program below.
C Program for Bit Stuffing
In the following source code of C programming in bit stuffing, while loop is used.
Firstly, the user needs to input the number of frame sizes than the bit string and the 0 is stuffed after every 5 consecutive 1’s execution.
Implementation of Bit stuffing Using C.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include <conio.h> #include <string.h> void main() { int x[50], y[50], n; int i, j, k, count = 1; printf("Enter size of a bit string:"); scanf("%d", &n); printf("Enter the bit string(0's &1's):"); for (i = 0; i < n; i++) { scanf("%d", &x[i]); } i = 0; j = 0; while (i < n) { if (x[i] == 1) { y[j] = x[i]; //count is less than 5 as 0 is inserted after every //5 consecutive 1's for (k = i + 1; x[k] == 1 && k < n && count < 5; k++) { j++; y[j] = x[k]; count++; if (count == 5) { j++; y[j] = 0; } i = k; } } else { y[j] = x[i]; } i++; j++; } //Displaying final result printf("Result of Bit Stuffing:"); for (i = 0; i < j; i++) { printf("%d", y[i]); } getch(); } |
The output of Bit Stuffing Program in C: