Bit stripping cpp

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.3 kB
2
Indexable

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;



string bitStuffing(int size, string data)
{

     string ans="";
     
     for(int i =0;i<size;i++){
     	if(data[i] == '1'){
     		int count = 0;
     	
     		while(count<5 && data[i] =='1'){
     			ans+=data[i];
     			count++;
     			i++;
     			
			 }
			 if(count==5){
			         ans += '0';
			 }else{
			 	ans+=data[i];
			 	
			 }
			 
		 }
		 else{
		 	ans += data[i];
		 }
	 }
	 ans+= data[size-1];
	 return ans;

}

string bitUnstuffing(int size, string data)
{

     string ans="";
     
     for(int i =0;i<size;i++){
     	if(data[i] == '1'){
     		int count = 0;
     	
     		while(count<5 && data[i] =='1'){
     			ans+=data[i];
     			count++;
     			i++;
     			
			 }
			 if(count==5){
			        i++;
			 }else{
			 	ans+=data[i];
			 	
			 }
			 
		 }
		 else{
		 	ans += data[i];
		 }
	 }
	 ans+= data[size-1];
	 return ans;

}



int main()
{
	
     string data = "111111";
     string flag = "0111110";
    int size = data.length();
    cout<<"bit stuffing"<<endl;
    
	string temp = bitStuffing(size, data);
	string ans = flag+" "+ temp+" "+flag;
	cout<<ans<<endl;
	
	cout<<"bit Unstuffing"<<endl;
	int size2 = temp.length();	 
	string temp2 = bitUnstuffing(size2,temp);
	
	cout<<temp2<<endl;
	
	return 0;
}
Leave a Comment