Bit Stuffing

Hello Friends,

In this tutorial we are going to study the topic of “Bit Stuffing” in Java.

So a simple question that arises in our mind is – What does Bit Stuffing means ?

In simple words when we communicate with other systems then there is a transmission of data.

Sometimes, the system may not differentiate between the beginning and end of the message due to the content

of the data may appear in the beginning as well as in the end.

To prevent confusion in data transmission, we use the concept of ‘Bit Stuffing.’ Various patterns can mark the difference between the actual data and the beginning or end of the transmission.

 

For ex.

We have to send 01111110 & if your message includes the same then your system will not be able to distinguished between them.

 

Here are the steps for performing Bit Stuufing:

class BitStuff {
     public static String btstf(String value)
     {
          StringBuilder stf = new StringBuilder();
          int count = 0;
 
          for(char i : value.toCharArray()) 
          {
              stf.append(i);

              if(i == '1')
              {
                   count++;

                   if(count == 5)
                   {
                        stf.append('0');
                        count = 0;
                   }
              }
              else
              {
                  count = 0;
              }
          }
          return stf.toString();
     }

       public static void main(String[] args)
       {
             String value = "11111011111";
             String stf = btstf(value);

             System.out.println("The Real Value Before Bit Stuffing Was: " + value);
             System.out.println("The Value of Stuffed Data: " + stf);
       }
}

 

The following section explains the code:

First we have used StringBuilder to built our new bit String.

After that we have looped through each bit by checking the bit of the input data one by one.

We defined a variable called count to track the number of consecutive 1s.

Next step is, to add 0 from the right side of five consecutive 1s.

Print the real data and the stuffed data that we performed by using this program.

 

So that’s a simple task that we learned in this tutorial.

Hope you are all cleared with the things that we performed in this tutorial.

 

Thanks!

Leave a Comment

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

Scroll to Top