-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHammingCode.cpp
More file actions
70 lines (69 loc) · 970 Bytes
/
Copy pathHammingCode.cpp
File metadata and controls
70 lines (69 loc) · 970 Bytes
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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cout<<"Enter the number of bits\n";
cin>>n;
int m1 = log2(n)+1;
int m2 = n+log2(n+m1)+1;
vector<int> v(m2,0);
cout<<"Enter the bits"<<endl;
int ind = 2,mask = 4,j,m;
while(ind<m2)
{
cin>>v[ind];
m = 1;
while(m<=ind+1)
{
if(m&(ind+1))
{
v[m-1]^=v[ind];
}
m = m<<1;
//j++;
}
ind++;
if(ind==mask-1)
{
ind++;
mask = mask<<1;
}
}
int pos;
cout<<"Enter the position where you want to introduce error\n";
cin>>pos;
v[pos-1] = !v[pos-1];
pos = 0;
vector<int> v1(m2-n,0);
mask = 1;
for(int i=0;i<m2;i++)
{
if(i==mask-1)
{
mask = mask<<1;
continue;
}
m = 1;j = 0;
while(m<=i+1)
{
if(m&(i+1))
{
v1[j]^=v[i];
}
m = m<<1;
j++;
}
}
for(int i=0;i<m2-n;i++)
{
if(v1[i]!=v[(1<<i)-1])
{
pos+=(1<<i);
}
}
cout<<"ERROR at: "<<pos<<endl;
return 0;
}