Bit stuffing program in Python
In this post, you will learn about the bit stuffing using the Python programming language.
Bit stuffing refers to the insertion of one or more bits into a data transmission as a way to provide signaling information to a receiver.
In the OSI model, the size of the data frames in variable-length frames may vary. In such cases, it's very difficult to detect the end and beginning of a frame. Hence, bit stuffing is used to mark the end and beginning of the frames during variable-length data frame transfer.
Bit stuffing is a process of inserting an extra bit as 0, once the frame sequence encountered 5 consecutive 1's. In the given source code of Python programming, while loop is used for bit stuffing.
# stuffing the stuffed signal
def stuff(sig):
onecounter = 0
index = 0
one = [] # one indexes
signal = list(sig)
for i in signal:
index += 1
if i == '0':
onecounter = 0
else:
onecounter += 1
if onecounter == 5:
one.append(index)
onecounter = 0
k = 0 # count extra index number
for i in one:
# print(i)
signal.insert(i + k, '0')
k += 1
return signal
# destuffing the stuffed signal
def destuff(sig):
onecounter = 0
index = 0
one = []
sig = list(sig)
for i in sig:
index += 1
if i == '0':
onecounter = 0
else:
onecounter += 1
if onecounter == 5:
one.append(index)
onecounter = 0
k = 0 # count extra index number
for i in one:
# print(i)
sig.pop(i + k)
k -= 1
return sig
signal = input("Enter the signal: ")
print("Original Signal : ", signal)
stuffed = stuff(signal)
print("Stuffed Signal: ", end="")
print("".join([a for a in stuffed]))
destuffed = destuff(stuffed)
print("Destuffed Signal: ", end="")
print("".join([a for a in destuff]))
Output of the above code:
Enter the signal: 1011110111101
Original Signal : 1011110111101
Stuffed Signal: 1011110111101
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary