1.bit Stuffing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Experiment No.

1
Aim: To study the mechanism of Bit Stuffing Protocol of Data Link Layer.
Software: Matlab
Theory:
One of the function of data link layer is Data Link Control and to implement it, mechanism
of framing is used. Bit synchronization is implemented at physical layer to ensure that the sender
and receiver use the same bit durations and timing. While data link layer, pack bits into frames, so
that each frame is distinguishable from another. Framing in the data link layer separates a message
from one source to a destination, by adding a sender address and a destination address. The
destination address defines where the packet is to go; the sender address helps the recipient
acknowledge the receipt. When a message is divided into smaller frames, a single-bit error affects
only that small frame.
Following diagram shows the frame structure incorporated in Data Link Layer:
Flag

Address

Control

Data

Checksum

Flag

Entire data which is to be sent is divided into such smaller frames. In order to distinguish
each frame from another, an 8 bit flag pattern is added at the start and end of each frame. One of the
common pattern used for flag is '0 1 1 1 1 1 1 0'.
But sometimes it may happen that pattern used for flag could also be part of information. In
such case receiver while reading the data, assumes that this is the flag pattern and this is the end of
frame.
In such scenario, we need to somehow inform the receiver that this is not the end of the
frame. One of the method to implement this is by stuffing 1 single bit to prevent the pattern from
looking like a flag. The strategy is called 'Bit Stuffing'. In bit stuffing, if a 0 and five consecutive 1
bits are encountered, an extra 0 is added. This extra stuffed bit is eventually removed from the data
by the receiver. The extra bit is added after one 0 followed by five 1s regardless of the value of the
next bit. This guarantees that the flag field sequence does not inadvertently appear in the frame.
Following example will clear the idea of bit stuffing:

Flow Chart:

Code:
clc;
clear all;
close all;
b=input('please enter the data');
c=length(b);
for i=1:1:length(b)
x=0;
if (b(i)==0)
h=i;
for g=1:1:5
h=h+1;
if (h>c)
break;
else if (b(h)==1)
x=x+1
end
end
if (x==5)
u=[b(1:h) 0 b(h+1:c)];
b=u
l=length(b)
c=l;
end
end
end
end
Result:
please enter the data[0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0 0]
b=
Columns 1 through 16
0 1 1 1 1 1 0 1 0 0 0 1 1 0
Columns 17 through 21
1 1 1 0 0

l=
21
b=
Columns 1 through 16
0 1 1 1 1 1
Columns 17 through 22
1 1 1 0 0 0

l=
22
Conclusion:
In this practical, we have used bit stuffing protocol at the transmitter side to avoid the
false detection flag in the received data bits at the receiver side.

You might also like