CN - Experiment 1
Aim
To implement Bit Stuffing, Byte Stuffing, and Character Stuffing techniques using C/C++
language.
Theory
In data communication, stuffing is a technique used to differentiate data bits/bytes from control
information. It helps avoid misinterpretation of data that might look like control flags or
delimiters.
● Bit Stuffing
○ Used in bit-oriented protocols (e.g., HDLC).
○ After every sequence of five consecutive 1 bits, a 0 is inserted.
○ Receiver removes these stuffed bits.
● Byte Stuffing
○ Used in byte-oriented protocols.
○ Special escape characters are inserted before reserved bytes (e.g., flag bytes).
● Character Stuffing
○ Similar to byte stuffing, but works with text data.
○ A special escape sequence is used to differentiate control characters from data
characters.
Algorithm and/or Flowchart
Algorithm for Bit Stuffing
1. Start
2. Read the data bit stream.
3. Initialize counter for consecutive 1s = 0.
4. For each bit:
○ If bit is 1, increment counter; if counter reaches 5, insert 0 and reset counter.
○ If bit is 0, reset counter.
5. Output stuffed data.
6. End
Algorithm for Byte Stuffing
1. Start
2. Read the byte stream.
3. For each byte:
○ If byte equals FLAG or ESC, insert ESC before the byte.
4. Output stuffed data.
5. End
Algorithm for Character Stuffing
1. Start
2. Read the character string.
3. For each character:
○ If the character is a flag or escape character, insert escape character before it.
4. Output stuffed string.
5. End
Flowchart :
Code :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string bitStuffing(string data) {
string stuffed = "";
int count = 0;
for (char bit : data) {
stuffed += bit;
if (bit == '1') {
count++;
if (count == 5) {
stuffed += '0';
count = 0;
}
} else {
count = 0;
}
}
return stuffed;
}
vector<unsigned char> byteStuffing(vector<unsigned char> data, unsigned char
FLAG, unsigned char ESC) {
vector<unsigned char> stuffed;
stuffed.push_back(FLAG);
for (unsigned char byte : data) {
if (byte == FLAG || byte == ESC) {
stuffed.push_back(ESC);
}
stuffed.push_back(byte);
}
stuffed.push_back(FLAG);
return stuffed;
}
string charStuffing(string data, char FLAG, char ESC) {
string stuffed = "";
stuffed += FLAG;
for (char c : data) {
if (c == FLAG || c == ESC) {
stuffed += ESC;
}
stuffed += c;
}
stuffed += FLAG;
return stuffed;
}
int main() {
string bitData = "0111111011111011";
cout << "Original Bit Data: " << bitData << endl;
cout << "Bit Stuffed Data: " << bitStuffing(bitData) << endl << endl;
vector<unsigned char> byteData = {'A', 0x7E, 'B', 0x7D};
unsigned char FLAG = 0x7E, ESC = 0x7D;
auto stuffedBytes = byteStuffing(byteData, FLAG, ESC);
cout << "Byte Stuffed Data: ";
for (auto b : stuffedBytes) cout << hex << (int)b << " ";
cout << endl << endl;
string charData = "HELLO~WORLD}";
cout << "Character Stuffed Data: " << charStuffing(charData, '~', '}') <<
endl;
return 0;
}
Output
Discussion
● Bit stuffing ensures that the special flag sequence (e.g., 01111110) does not appear
accidentally in the data by inserting a 0 after five consecutive 1s.
● Byte stuffing uses an escape character to distinguish actual data bytes from control
bytes.
● Character stuffing works similarly but is applied to text strings.
● These techniques are vital for framing in data communication protocols.
Finding & Learning
● Learned the implementation differences between bit-oriented and byte/character-
oriented protocols.
● Understood how stuffing prevents misinterpretation of data as control information.
● Gained practical coding experience in implementing these protocols in C++.