-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamming.cpp
84 lines (75 loc) · 1.34 KB
/
hamming.cpp
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,temp = 1;
cout<<"Enter total data bits : ";
cin>>n;
vector<char> data(n+1);
int nd,np;
np = ceil(log2(n));
nd = n - np;
cout<<"Enter "<<nd<<" bits of data : ";
string data_bits;
int dptr = 0;
cin>>data_bits;
cout<<"Enter "<<np<<" parity bits : ";
string parity_bits;
int pptr = 0;
cin>>parity_bits;
for(int i = 1;i<=n;i++)
{
if(ceil(log2(i)) == floor(log2(i)) || (i) == 1)
{
data[i] = parity_bits[pptr];
pptr++;
}
else
{
data[i] = data_bits[dptr];
dptr++;
}
}
int t = 0;
for(int i = pow(2,t); i<=n;i = pow(2,t))
{
t++;
int p_xor = 0;
int cnt = 0;
//cout<<"i : "<<i<<" ";
for(int j = i;j<=n;j++)
{
cnt++;
//cout<<", j : "<<j;
p_xor = p_xor ^ (data[j] - '0');
if(cnt == i)
{
cnt = 0;
j = j + i;
}
}
//cout<<i<<" "<< p_xor<<endl;
if(p_xor == 1)
{
temp = 0;
cout<<"Parity error in : P"<<i<<" group"<<endl;
}
else
{
cout<<"No error in P"<<i<<" parity group"<<endl;
}
}
reverse(data.begin(),data.end());
cout<<endl;
cout<<"Data : "; for(auto c : data){cout<<c;}
cout<<endl;
if(temp == 1)
{
cout<<endl<<"\tNo Error Detected"<<endl;
}
else
{
cout<<endl<<"\tError Detected"<<endl;
}
return 0;
}