Bit Stuffing Program in Python
Here, you will get the example code of bit stuffing program in python programming.
Bit stuffing method is used in data communication to ensure data integrity and synchronization between the sender and receiver.
In this method, a special bit pattern is inserted into the data to prevent unintended interpretation of control characters as data or to ensure the receiver can distinguish between frames.
Example Code of Bit Stuffing Program in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | def bit_stuffing(data): stuffed_data = "" count = 0 for bit in data: if bit == "1": count += 1 stuffed_data += bit if count == 5: stuffed_data += "0" count = 0 else: count = 0 stuffed_data += bit return stuffed_data def bit_unstuffing(stuffed_data): data = "" count = 0 for bit in stuffed_data: if bit == "1": count += 1 data += bit else: count = 0 data += bit if count == 5: # Skip the stuffed zero bit count = 0 return data def main(): # Example usage data = "01111110" + "010" + "1101010101" + "01111110" print("Input Data:", data) stuffed_data = bit_stuffing(data) print("Stuffed Data:", stuffed_data) unstuffed_data = bit_unstuffing(stuffed_data) print("Unstuffed Data:", unstuffed_data) if __name__ == "__main__": main() |
The bit_stuffing function takes a string of binary data and inserts a “0” after every five consecutive “1” bits. The bit_unstuffing function reverses this process, removing the stuffed bits to retrieve the original data.
Output:
Program Explanation
- The Input Data is the binary data without any bit stuffing, represented by the string “011111100101101010101111110”.
- The Stuffed Data is the binary data after applying bit stuffing, represented by the string “011111100010110101010111110”. Here, a “0” bit is inserted after every five consecutive “1” bits to ensure synchronization and data integrity during transmission.
- The Unstuffed Data is the binary data after removing the stuffed bits from the Stuffed Data, represented by the string “011111100101101010101111110”. The original data is successfully retrieved after performing bit unstuffing.