-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorBitFinding&Correct.cpp
More file actions
91 lines (69 loc) · 1.64 KB
/
ErrorBitFinding&Correct.cpp
File metadata and controls
91 lines (69 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;
int main(){
int DataBit;
cout<<"\n\nHow many bits do you want to enter ? : ";
cin>>DataBit;
int sizee = DataBit;
int userBit[sizee];
for(int i=1;i<=sizee;i++){
cin>>userBit[i];
}
stack<int>bitNoError;
int pBit = 3;
// int pBit = 1;
while(pBit>=1){
vector<int>v;
int d = pBit-1;
for(int i=1;i<=sizee;i++){
if( (i & (1<<(d))) != 0){ // pBit th set asel tar vector madhye tak
v.push_back(userBit[i]);
}
}
int one_cnt = count(v.begin(),v.end(),1);
// cout<<"One count for "<<pBit<<"is "<<one_cnt<<endl;
if(one_cnt % 2 != 0){
int num = (1<<d);
userBit[num] = 1; // even parity ke liye change
bitNoError.push(1);
}
else{
bitNoError.push(0);
}
pBit--;
}
/* Calculating error bit number */
int ans = 0;
int i =0;
while(!bitNoError.empty()){
int num = (1<<i);
if(bitNoError.top()){
ans += num;
i++;
}
else{
i++;
}
bitNoError.pop();
}
if(ans){
cout<<"\nError Bit is : "<<ans<<endl;
cout<<"Errorless hamming code : \n";
for(int i=1;i<=sizee;i++){
if(i==ans){
if(userBit[i]==1){
cout<<0<<" ";
}else{
cout<<1<<" ";
}
}
else{
cout<<userBit[i]<<" ";
}
}cout<<endl;
}
else{
cout<<"\nNo Error in Data."<<endl;
}
return 0;
}