Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Bit Stuffing Program in C

The whole world is running on data. Data is the most precious asset of any business, organization, or industry. We cannot just produce the data, as data also leads to some problems, like storing the data, analyzing the data, transferring the data, etc. While transferring the data we have to take precautions for many steps like we have to make sure the data is not corrupted, there is no data loss, and to ensure all these we follow certain algorithms or methods, and one of the methods is bit stuffing. While proceeding further in this article we will learn about the bit stuffing program in c, along with its applications.

What is Bit Stuffing?

Bit stuffing is one of the methods of transmitting data over the communication channel. The bit stuffing ensures that the receiver can distinguish the actual data from the overall data that contains framing and control channels. We use this technique to prevent data corruption, loss, and confusion during transmission.

Bit stuffing is the process of adding one or more than one extra bit into the data packet to differentiate it from the control characters. We use this technique mostly in serial communication systems. In serial communication systems, the data is transferred in the form of a continuous stream of bits. We divide the data into various frames or packets. Each packet contains a predefined number of bits. The frame consists of a header, actual data, and a trailer.

The trailer and header are known as the control characters and they are used to denote the end and start of the node. They are specific bit patterns that are not likely to occur in actual data. They are used as they make the receiver easily identify the start and end of the actual data in the packet. That is useful in extracting the actual data from the total data.

But this will also create a problem in the case then the actual data and the control characters contain the same bit pattern. As of now the receiver will be confused and might interpret the data as the control characters and then ignore the data which will lead to data loss or corruption. We use bit stuffing to overcome this problem.

In bit stuffing, we use a specific bit pattern to indicate the start and end of the packet. And as explained in the above case that there might be a scenario when the control characters and the actual data contain the same bit pattern in this case the bit stuffing insert an extra bit into the data to differentiate it from the control characters.
In bit stuffing, we add an extra bit after every 5 consecutive 1’s in the data. This will ensure that the receiver will now be able to distinguish the actual data from the control characters.

Bit Stuffing Program in C

Now we will see a bit stuffing program in c with its code and implementation and explanation of the above code.

#include <stdio.h>
#include <string.h>
 
void bitStuffing(int N, int arr[])
{
    // Here we will store the stuffed array.
    int brr[30];
 
 
    int i, j, k;
    i = 0;
    j = 0;
 
    while (i < N) {
 
        if (arr[i] == 1) {
 
            //Will store the count of consecutive ones
            int count = 1;
 
 
            brr[j] = arr[i];
 
 
            for (k = i + 1;
                arr[k] == 1 && k < N && count < 5; k++) {
                j++;
                brr[j] = arr[k];
                count++;
 
                // if we found 5 consecutive ones then will insert a zero.
                if (count == 5) {
                    j++;
                    brr[j] = 0;
                }
                i = k;
            }
        }
 
    // otherwise will copy the array directly without any modification.
        else {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
 
    for (i = 0; i < j; i++)
        printf("%d", brr[i]);
}
 
 
int main()
{
    int N = 9;
    int arr[] = { 1, 1, 1, 0, 1, 1, 1, 1, 1 };
 
    bitStuffing(N, arr);
 
    return 0;
}

Output

1110111110

Explanation of the above code
In the above program we are traversing the original data array and after that when we encounter a set bit we will count the number of consecutive set bits if the theory is 5 then we will insert a zero bit at their end and then repeat the operation for the remaining array.

Applications of Bit Stuffing

Bit Stuffing has many applications some of which are mentioned below:

  • It is used in network communication protocols like HDLC, and Ethernet as it ensures reliable data transmission.
  • It is used in serial communication protocols like USB and RS-232 to prevent data loss due to synchronization errors.
  • We can use bit stuffing in file transferring protocols as they allow us to transfer the data without errors.
  • The synchronization in bit stuffing allows it to be used in image and video processing.

Conclusion
In conclusion, bit stuffing is a technique used in communication systems to prevent data loss or corruption during transmission. It involves inserting one or more extra bits into a data packet to differentiate it from the control characters. Bit stuffing is implemented using bitwise operators in C programming language. Bit stuffing has several applications in communication systems, including the transmission of network packets over Ethernet and the transmission of data over USB.

Frequently Asked Questions

Here are some of the frequently asked questions about the bit stuffing program in c.

1. What are control characters?
Control characters are specific bit patterns used in communication systems to indicate the start and end of data packets or to control the flow of data.

2. What is the difference between byte stuffing and bit stuffing?
Byte stuffing involves adding an extra byte to the data packet, whereas bit stuffing involves adding an extra bit to the data packet.

3. What are bitwise operators?
Bitwise operators are operators used in C to perform operations on individual bits within a byte.

4. How is bit stuffing used in Ethernet?
Ethernet frames use a specific bit pattern called the preamble and start of frame delimiter (SFD) to indicate the start of a packet. Bit stuffing is used to ensure that the data in the packet does not contain the same bit pattern as the preamble and SFD.

5. How is bit stuffing used in USB?
USB uses bit stuffing to ensure that the data does not contain the same bit pattern as the USB packet synchronization characters.

Leave a Reply

Your email address will not be published. Required fields are marked *