Skip to Content

Practical 6

AIM:

Write a program to demonstrate the concept of bit stuffing.

Program Code (python):

def bit_stuff(data): stuffed = "" count = 0 for bit in data: if bit == "1": count += 1 stuffed += bit if count == 5: stuffed += "0" # Insert '0' after five consecutive '1's count = 0 else: count = 0 stuffed += bit return stuffed data = input("Enter binary data: ") stuffed_data = bit_stuff(data) print("Original Data:", data) print("Stuffed Data:", stuffed_data)

Sample Output:

Enter binary data: 1111101111101111 Original Data: 1111101111101111 Stuffed Data: 1111100111110111110

Code (C)

//BIT STUFFING #include <stdio.h> int main() { int i = 0, j = 0, count = 0; char data[10] = "1011111010"; char stuffed[50]; for (i=0;i<10;i++) { stuffed[j++] = data[i]; if (data[i] == '1') { count++; if (count == 5) { // If five consecutive 1s, stuff a 0 stuffed[j++] = '0'; count = 0; } } else { count = 0; } } printf("Original Data: %s\n", data); printf("Stuffed Data: %s\n", stuffed); return 0; }
Last updated on