Department of
Computer Science and Engineering
Title: Implementing Bit Stuffing and De-stuffing
Data Communication Lab
CSE 308
Green University of Bangladesh
1 Objective(s)
• To implement the data link layer framing method bit stuffing.
2 Problem analysis
A technique that allows data frames to contain arbitrary number of bits and allows character codes with arbitrary
number of bits per character. Bit stuffing is which an zero bit is stuffed after five consecutive ones in the
input bit stream. On the other hand, Bit de-stuffing is the process of removing the stuffed bit in the output
stream.
To provide service to network layer, the data link layer, must use the services provided to it by the physical
layer. The bit stream is not guaranteed to be error free. The number of bits received may be less than, equal
to, or more than data link layer to detect and, if necessary, correct errors.
The usual approach is for the data link layer to break the bit stream up into discrete frames and compute
the checksum for each frame. When a frame arrives at the destination, the checksum is re computed. If the
newly computed checksum is different from one contained in the frame, the data link layer knows than an error
has occurred and takes steps to deal it.
Each frame begins and ends with a special bit pattern, [Link] ever the sender’s data link layer
encounter five consecutive 1’s in the data, it automatically stuffs a 0 bit in to outgoing bit stream. This bit
stuffing is analogous to byte stuffing. When ever the receiver sees five consecutive incoming ones, followed by a
0 bit, it automatically de-stuffs the 0 bit.
Figure 1: Bit studding and de-stuffing in HDLC frame structure.
3 Algorithm
Algorithm 1: Bit stuffing
Input: Given an array, arr[] of size N consisting of 0’s and 1’s.
/* The idea is to check if the given array consists of 5 consecutive 1’s. Follow the
steps below to solve the problem: */
1 Initialize the array brr[] which stores the stuffed array. Also, create a variable count which maintains
the count of the consecutive 1’s.
2 Traverse in a while loop using a variable i in the range [0, N) and perform the following tasks:
• If arr[i] is 1 then check for the next 4 bits if they are set bits as well. If they are, then insert a 0
bit after inserting all the 5 set bits into the array brr[].
• Otherwise, insert the value of arr[i] into the array brr[].
Algorithm 2: Bit de-stuffing
Output: Given an array, arr[] of size N consisting of 0’s and 1’s.
/* The idea is to check if the given array consists of 5 consecutive 1’s. Follow the
steps below to solve the problem: */
• skip the next bit in the array arr[] in place of inserting a 0 bit in the array brr[].
4 Implementation in C
4.1 Bit Stuffing
1 #include <stdio.h>
2 #include <string.h>
3
4
5 // Function for bit stuffing
6 void bitStuffing(int N, int arr[])
7 {
8 int brr[30]; // Stores the stuffed array
9
10 int i, j, k; // Variables to traverse arrays
11 i = 0;
12 j = 0;
13 int count = 1; // Stores the count of consecutive ones
14
15 // Loop to traverse in the range [0, N)
16 while (i < N) {
17
18 // If the current bit is a set bit
19 if (arr[i] == 1) {
20
21 brr[j] = arr[i]; // Insert into array brr[]
22
23 // Loop to check for next 5 bits
24 for (k = i + 1; arr[k] == 1 && k < N && count < 5; k++)
25 {
26 j++;
27 brr[j] = arr[k];
28 count++;
29
30 // If 5 consecutive set bits are found insert a 0 bit
31 if (count == 5) {
32 j++;
33 brr[j] = 0;
34 }
35 i = k;
36 }
37 }
38
39 // Otherwise insert arr[i] into the array brr[]
40 else {
41 brr[j] = arr[i];
42 }
43 i++;
44 j++;
45 }
46
47 // Print Answer
48 for (i = 0; i < j; i++)
49 printf("%d", brr[i]);
50 }
51
52 // Driver Code
53 int main()
54 {
55 int N = 6;
56 int arr[] = { 1, 1, 1, 1, 1, 1 };
57
58 bitStuffing(N, arr);
59
60 return 0;
61 }
4.2 Bit De-stuffing
1 #include <stdio.h>
2 #include <string.h>
3
4 // Function for bit de−stuffing
5 void bitDestuffing(int N, int arr[])
6 {
7 int brr[30]; // Stores the de−stuffed array
8
9 int i, j, k; // Variables to traverse the arrays
10 i = 0;
11 j = 0;
12
13 int count = 1; // Stores the count of consecutive ones
14
15 // Loop to traverse in the range [0, N)
16 while (i < N) {
17
18 // If the current bit is a set bit
19 if (arr[i] == 1) {
20
21 brr[j] = arr[i]; // Insert into array brr[]
22
23 // Loop to check for the next 5 bits
24 for (k = i + 1; arr[k] == 1 && k < N && count < 5; k++) {
25 j++;
26 brr[j] = arr[k];
27 count++;
28
29 // If 5 consecutive set bits are found skip the next bit
30 //in arr[]
31 if (count == 5) {
32 k++;
33 }
34 i = k;
35 }
36 }
37
38 // Otherwise insert arr[i] into the array brr
39 else {
40 brr[j] = arr[i];
41 }
42 i++;
43 j++;
44 }
45
46 // Print Answer
47 for (i = 0; i < j; i++)
48 printf("%d", brr[i]);
49 }
50
51 // Driver Code
52 int main()
53 {
54 int N = 7;
55 int arr[] = { 1, 1, 1, 1, 1, 0, 1 };
56
57 bitDestuffing(N, arr);
58
59 return 0;
60 }
5 Input/Output (Compilation,Debugging & Testing)
Stuffing:
Input: 111111
Output: 1111101
De-stuffing:
Input: 1111101
Output: 111111
6 Discussion & Conclusion
Based on the focused objective(s) to understand about bit stuffing and de-stuffing, the additional lab exercise
made me more confident towards the fulfilment of the objectives(s).
7 Lab Task (Please implement yourself and show the output to the
instructor)
• Implement the bit stuffing and de-stuffing together where the system provides a choice to change the
transmitted bit stream before de-stuffing.
8 Lab Exercise (Submit as a report)
• Complete the given lab task and submit as a report.
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.