×

Bit Stuffing Program in C

What is a Bit Stuffing?

The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are the non-information bits.

Why Bit stuffing is used?

The OSI model's variable-length frames can have data frames of varying sizes. In such cases, it might be extremely difficult to pinpoint the beginning and end of a frame. Bit stuffing is therefore used to signal the end and beginning of the frames during variable-length data frame transfer.

The bit stuffing application can be used to quickly create new files and folders. With only one click, you may utilize the application to create new files or directories.

Process in Bit Stuffing

With the new method known as "bit stuffing," character codes can have any number of bits per character, and data frames can include any number of bits.A flag byte, also known as the unusual bit pattern 01111110, begins and ends each frame.

When the data link layer of the sender encounters five consecutive 1s, it immediately adds a 0 bit to the outgoing bit stream.

Complexities of bit stuffing in c

Complexity of Time

O(N), where "N" is the array's size "arr."

The array is only once traversed, hence the time complexity is O. (N).

Complexity of Space

O(N), where "N" is the array's size "arr."

As we are merely initializing the vector "ANS," which has a size of "N." The space complexity is therefore O. (N).

Example 1

N = 6, arr[] = 1, 1, 1, 1, 1, 1 as an input

Results: 1111101

Explanation: After the fourth index in the given array, there are 5 consecutive 1s encountered while traversing the array. As a result, after the fourth index, a zero bit has been added to the packed array.

Example 2

N = 6, arr[] = 1, 0, 1, 0, 1, 0 as an input.

Result: 101010

The approach in Bit Stuffing

The goal is to determine whether the provided array has 5 consecutive 1s.

There are two steps to be followed for the  approach of bit stuffing in c

They are

  • Initializing the array
  • Traversing in a while loop

Initializing the array

set the array brr[], which houses the packed array, to zero. Make a variable count that keeps track of the number of consecutive 1s

Traversing in a while loop

The following actions using a variable iin the range [0, N] and a while loop:

If arr[i] is 1, then see if the next 4 bits are also set. If they are, add a 0 bit to the array brr[after adding all 5 set bits].

If not, then in that case add the value of arr[i] to the brr[ array ].

Software and hardware requirements

We have software and hardware requirements for the execution of bit stuffing in c programming

The software requirements are given as intel-based desktop pc: RAM of 1GB

The Hardware requirements aregiven as Turbo c or Turbo C++

Example programs

Example 1

// C program for the bit stuffing 
#include <stdio.h>
#include <string.h>
 void bitStuffing(int n, int arr[])
{
    int brr[40];
    int i, j, k;
i = 0;
    j = 0;
    int count = 1;
    while (i< n)
    {
    if (arr[i] == 1) 
    {
        brr[j] = arr[i];
     for (k = i + 1;arr[k] == 1 && k < n && count < 5;k++)
     {
j++;
                brr[j] = arr[k];
                count++;
             if (count == 5) 
             {
j++;
                    brr[j] = 0;
                }
i = k;
            }
        }
        else
        {
            brr[j] = arr[i];
        }
i++;
j++;
    }
    for (i = 0; i< j; i++)
printf("%d", brr[i]);
}
 int main()
{
    int n = 6;
    int arr[] = { 1, 1, 1, 1, 1, 1 };


bitStuffing(n, arr);


    return 0;
}

Output

1111101

Example 2

#include<stdio.h>
#include<string.h>
int main()
{
    int a[20],b[30],i,j,k,count,n;
printf("Enter frame size :");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
    count=1;
    j=0;
    while(i<n)
    {
        if(a[i]==1)
        {
            b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
            {
j++;
                b[j]=a[k];
                count++;
                if(count==5)
                {
j++;
                    b[j]=0;
                }
i=k;
            }
        }
        else
        {
            b[j]=a[i];
        }
i++;
j++;
    }
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
    return 0;
}

Output

Enter frame size : 8
8
Enter the frame in the form of 0 and 1 :1 0 1 0 1 1 0 0 
After Bit Stuffing :10101100

Related Topics

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.