-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamming.cpp
More file actions
170 lines (150 loc) · 4.68 KB
/
Copy pathhamming.cpp
File metadata and controls
170 lines (150 loc) · 4.68 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <cstring>
#include <vector>
#include <math.h>
using namespace std;
int main() {
string binaryString;
vector<char> v;
while (true) {
cout << "Enter the Binary Number to be transmitted: ";
cin >> binaryString;
copy(binaryString.begin(), binaryString.end(), back_inserter(v));
int wrongBinary = 0;
for (int i = 0; i < v.size(); ++i) {
if (isdigit(v[i]) && ((v[i] - '0') == 0 || (v[i] - '0') == 1)) {
continue;
} else {
wrongBinary = 1;
break;
}
}
if (wrongBinary == 1) {
cout<<"Please enter a proper Binary Number to be sent"<<endl;
continue;
} else {
break;
}
}
int initialSizeOfBinary = v.size();
int lastUsedPowerOfTwo = pow(2,0);
int finalSizeOfBinary;
int extrabits = 0;
int i = 1;
while(initialSizeOfBinary>0){
int currentlyUsedPowerOfTwo = pow(2, i);
int difference = currentlyUsedPowerOfTwo - lastUsedPowerOfTwo;
extrabits++;
if(difference-1 <= initialSizeOfBinary){
initialSizeOfBinary = initialSizeOfBinary - (difference - 1);
if(initialSizeOfBinary>0)
//finalSizeOfBinary++;
lastUsedPowerOfTwo = currentlyUsedPowerOfTwo;
}
else{
//finalSizeOfBinary = finalSizeOfBinary + initialSizeOfBinary;
break;
}
i++;
}
// cout<<"Initial Size: "<<v.size()<<endl;
// cout<<"Final Size: "<<extrabits+v.size()<<endl;
// cout<<"Last Used Power of two : "<<lastUsedPowerOfTwo<<endl;
finalSizeOfBinary = extrabits + v.size();
int finalBinaryData[finalSizeOfBinary];
i = 0;
int index2 = 0;
while(i<finalSizeOfBinary){
int j = 0;
int powersOfTwo = 0;
int isIndexEqualPower = 0;
while(powersOfTwo<=lastUsedPowerOfTwo){
powersOfTwo = pow(2, j);
if(i+1 == powersOfTwo){
isIndexEqualPower = 1;
break;
}
j++;
}
if(isIndexEqualPower == 1){
finalBinaryData[i] = -1;
} else{
finalBinaryData[i] = v[index2] - '0';
index2++;
}
i++;
}
// for (int k = 0; k < finalSizeOfBinary; ++k) {
// cout<<finalBinaryData[k]<<endl;
// }
int powersOfTwo = pow(2, 0);
int j = 0;
while(powersOfTwo<=lastUsedPowerOfTwo) {
int incrementBy = pow(2, j+1);
int startOfBlock = powersOfTwo - 1;
int parityBit = 0;
for (int k = 0; k < powersOfTwo; ++k) {
for (int l = startOfBlock; l < finalSizeOfBinary; l = l + incrementBy) {
if(finalBinaryData[l] != -1){
parityBit = parityBit ^ finalBinaryData[l];
}
}
startOfBlock++;
}
finalBinaryData[powersOfTwo - 1] = parityBit;
j++;
powersOfTwo = pow(2, j);
}
cout<<"Final data: "<<endl;
for (int m = 0; m < finalSizeOfBinary; ++m) {
cout<<finalBinaryData[m]<<" ";
}
vector<char> v2;
vector<int> binaryInInteger;
while (true) {
cout<<"Please enter what you received : ";
cin >> binaryString;
copy(binaryString.begin(), binaryString.end(), back_inserter(v2));
int wrongBinary = 0;
for (int i = 0; i < v2.size(); ++i) {
if (isdigit(v2[i]) && ((v2[i] - '0') == 0 || (v2[i] - '0') == 1)) {
binaryInInteger.push_back(v2[i] - '0');
continue;
} else {
wrongBinary = 1;
break;
}
}
if (wrongBinary == 1) {
cout<<"Please enter a proper Binary Number to be sent"<<endl;
continue;
} else {
break;
}
}
int index = 0;
powersOfTwo = pow(2, 0);
int indexAtWhichError = -1;
while(powersOfTwo < binaryInInteger.size()){
int incrementBy = pow(2, index+1);
int startOfBlock = powersOfTwo - 1;
int parityBit = 0;
for (int k = 0; k < powersOfTwo; ++k) {
for (int l = startOfBlock; l < binaryInInteger.size(); l = l + incrementBy) {
parityBit = parityBit ^ binaryInInteger[l];
}
startOfBlock++;
}
if(parityBit == 1){
indexAtWhichError = indexAtWhichError + powersOfTwo;
}
index++;
powersOfTwo = pow(2,index);
}
if(indexAtWhichError == -1){
cout<<"No error in transmission"<<endl;
} else{
cout<<"Error is at bit number "<<indexAtWhichError;
}
return 0;
}