-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhammingCode.cpp
More file actions
76 lines (72 loc) · 2.46 KB
/
hammingCode.cpp
File metadata and controls
76 lines (72 loc) · 2.46 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
#include <bits/stdc++.h>
#include <ctime>
using namespace std;
string xorValues(string st, vector<int> positions)
{
int n = positions.size();
int sizeOfSt = st.size();
//0,1,3,7;
int xorValue = st[sizeOfSt - positions[0]] - '0';
for (int i = 1; i < n; i++)
{
xorValue = xorValue ^ (st[sizeOfSt - positions[i]] - '0');
}
return to_string(xorValue);
}
string dataToHammingCode(string data)
{
data = data + '-' + '-';
data = data.substr(0, data.size() - 3) + '-' + data.substr(data.size() - 3, data.size());
data = data.substr(0, data.size() - 7) + '-' + data.substr(data.size() - 7, data.size());
int size = data.size();
vector<int> positions{3, 5, 7, 9, 11};
data = data.substr(0, size - 1) + xorValues(data, positions);
positions.clear();
positions = {3, 6, 7, 10, 11};
data = data.substr(0, size - 2) + xorValues(data, positions) + data.substr(size - 1);
positions.clear();
positions = {5, 6, 7};
data = data.substr(0, size - 4) + xorValues(data, positions) + data.substr(size - 3);
positions.clear();
positions = {9, 10, 11};
data = data.substr(0, size - 8) + xorValues(data, positions) + data.substr(size - 7);
return data;
}
string checkError(string data)
{
vector<int> positions{3, 5, 7, 9, 11};
string errorPosition = xorValues(data, positions);
positions.clear();
positions = {3, 6, 7, 10, 11};
errorPosition = errorPosition + xorValues(data, positions);
positions.clear();
positions = {5, 6, 7};
errorPosition = errorPosition + xorValues(data, positions);
positions.clear();
positions = {9, 10, 11};
errorPosition = errorPosition + xorValues(data, positions);
return errorPosition;
}
int main()
{
srand(time(0));
string data = "1011001";
cout << "\nOriginal data to be sent: " << data;
int numberOfOnes = 0;
for (int i = 0; i < data.size(); i++)
{
if (data[i] == '1')
numberOfOnes++;
}
string encryptedData = dataToHammingCode(data);
cout << "\nAfter adding the parity bits (using Hamming Code's method): " << encryptedData;
//make a random change in the encrypted data due to noise:
cout << "\nSimulating a probable noise... ";
int randomIndex = rand() % encryptedData.size();
encryptedData[randomIndex] = encryptedData[randomIndex] == '1' ? '0' : '1';
cout << "\nChecking the received message for the error...";
string errorPosition = checkError(encryptedData);
int integerPosition = stoi(errorPosition, 0, 2);
cout << "\nThe error is at position: " << integerPosition;
return 0;
}