CN-LAB-V-SEM

Exercise 13: Implement Bit Stuffing Framing Method to Resolve Framing Errors

Bit Stuffing Program

#include <stdio.h>
#include <string.h>

int main() {
    char inp[50], stuffed[100];
    int len, i, count = 0, j = 0;

    printf("Enter the data: ");
    scanf("%s", inp);

    len = strlen(inp);
    for (i = 0; i < len; i++) {
        stuffed[j++] = inp[i];
        if (inp[i] == '1') {
            count++;
        } else {
            count = 0;
        }

        if (count == 5) {
            stuffed[j++] = '0';
            count = 0;
        }
    }
    
    stuffed[j] = '\0';
    printf("Data after Bit Stuffing: %s\n", stuffed);

    return 0;
}

Output Example

Enter the data: 11011111
Data after Bit Stuffing: 110111110

Explanation

This program demonstrates the Bit Stuffing Framing Method, which is used to avoid ambiguity in communication systems when certain patterns, such as consecutive 1s, may be mistaken for frame delimiters.

Steps

  1. Input: The user provides a binary string as input.
  2. Bit Stuffing Logic:
    • The program processes the string and inserts a 0 after five consecutive 1s.
    • This is done to prevent the occurrence of six consecutive 1s, which could be interpreted as a frame delimiter.
  3. Output: The program outputs the modified binary string with inserted 0s to break up sequences of five consecutive 1s.

Use Case

Bit stuffing is commonly used in protocols like HDLC to ensure that control characters (like 011111 or 111111) do not appear in the data portion of the transmission, maintaining the integrity of the communication.