×

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

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.