Name: Kshitij Chandrakar
Batch: DS 5
SAP: 500124827
Theoretical selection #
Bit Stuffing: Bit stuffing is a technique used in data transmission to avoid confusion between data and control characters. It involves inserting a non-data bit (usually a 0) after a specific pattern of consecutive bits (e.g., 5 consecutive 1s). This prevents the occurrence of reserved patterns, like flags or control sequences, within the actual data stream.
Bit Stuffing vs Byte Stuffing:
- Bit Stuffing: Involves inserting a bit (usually 0) after a specific number of consecutive 1s to prevent the flag pattern from appearing in the data.
- Example: If the flag is
01111110
, and the data contains11111
, a 0 is inserted after the fifth 1, changing it to111110
.
- Example: If the flag is
- Byte Stuffing: Involves inserting a special byte (e.g.,
0x7E
) to represent reserved control characters when they appear in the data stream.- Example: If
0x7E
is the flag, and the data contains0x7E
, it would be stuffed with another byte, like0x7D 0x5E
.
- Example: If
- Bit Stuffing: Involves inserting a bit (usually 0) after a specific number of consecutive 1s to prevent the flag pattern from appearing in the data.
Bit Stuffing (assuming
011111
is the flag sequence):- Original bit stream:
01111110 110111111011111010
- Stuffed bit stream:
01111110 110111111011111010
→ Insert a0
after five consecutive 1s.- Result:
01111110 110111111011111010
becomes01111110 1101111101111101010
- Result:
Destuffing:
- Take the stuffed stream and remove the 0 after five consecutive 1s.
- Result:
01111110 110111111011111010
(original).
- Result:
- Original bit stream:
Advantages and Disadvantages of Bit Stuffing:
- Advantages:
- Prevents data sequences from mimicking control sequences like flags.
- Ensures reliable data transfer without control character conflicts.
- Disadvantages:
- Adds overhead by increasing the size of the transmitted data.
- Increases complexity in both encoding and decoding.
- Advantages:
Practical Section #
Code #
Stuffing Function #
def Stuff(inp = "110111111011111010"):
stuff = ""
c = 0
print("Input is:", inp)
for bit in inp:
if bit == "1":
c += 1
else:
c = 0
stuff += bit
if c == 5:
stuffed += "0"
c = 0
print("Bit Stuffed Output is:", stuff)
return stuff
Destuffing Function #
def Destuff(inp = ""):
print("Stuffed Input is", inp)
destuff = ""
c = 0
i = 0
while i < len(inp):
destuff += inp[i]
if inp[i] == "1":
c += 1
else:
c = 0
if c == 5:
i += 1
c = 0
i += 1
print("Bit Destuffed Output is:", destuff)
return destuff
Main #
def main():
inp = input("Enter Input (Blank for default): ")
if inp != "":
print("-------------In-------------")
out = Stuffing(inp=inp)
print("-------------Out-------------")
Destuff(inp=out)
else:
print("Using Defaults")
print("-------------In-------------")
out = Stuff(inp="011111")
print("-------------Out-------------")
Destuff(inp=out)
pass
if __name__ == '__main__':
main()
Output #
