-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammingCode.cpp
More file actions
204 lines (189 loc) · 4.11 KB
/
HammingCode.cpp
File metadata and controls
204 lines (189 loc) · 4.11 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//============================================================================
// Name : HammingCode.cpp
// Author : SidPro
// Version : 1.0
// Description :
/*
=> Hamming code is a block code that is capable of detecting up to
two simultaneous bit errors and correcting single-bit errors.
=> In this coding method, the source encodes the message by inserting redundant bits within the message.
These redundant bits are extra bits that are generated and inserted at specific positions in the message
itself to enable error detection and correction. When the destination receives this message, it performs
recalculations to detect errors and find the bit position that has error.
=> https://www.geeksforgeeks.org/hamming-code-in-computer-network/
=> https://en.wikipedia.org/wiki/Hamming_code
=> https://www.tutorialspoint.com/error-correcting-codes-hamming-codes
*/
//============================================================================
/* input - output format
*
* Enter Your message here: 10101010100
Which parity is in even or odd [ e / d ]? e
Bit calculation:
1010101R010R0RR
R1 = 1111000 = 0
R2 = 1010010 = 1
R4 = 1010010 = 1
R8 = 1010101 = 0
Hamming Code: 101010100101010
Reciver Side decoding: 0000
*/
#include <iostream>
#include <cmath>
#include <tuple>
#include <algorithm>
using namespace std;
tuple<string,int> Sender(string ,char);
void Reciver(string,int, char);
int main() {
string mesg;
char parity;
int r;
cout<<"Enter Your message here: ";
cin>>mesg;
cout<<"Which parity is in even or odd [ e / d ]? ";
cin>>parity;
tie(mesg,r)=Sender(mesg,parity);
// mesg = "10101101110";
Reciver(mesg,r,parity);
return 0;
}
tuple<string,int> Sender(string mesg,char parity){
string midmsg = "22";
reverse(mesg.begin(),mesg.end());
int base=2,r,i,j,m = mesg.length();
for(r=0;r<31;++r){
if(pow(2,r)>=m+r+1)
break;
}
string redundant[r];
string redunt[r];
i=0;
while(i<m){
if((i+1+base)==pow(2,base)){
midmsg+='2';
++base;
}
else{
midmsg+=mesg[i];
++i;
}
}
for(j=0;j<r;++j){
int temp = pow(2,j);
for(i=temp-1;i<m+r;i=i+temp){
int k = temp+1;
while(--k&&i<m+r){
if(midmsg[i]!='2')
redundant[j]+=midmsg[i];
++i;
}
}
}
for(i=0;i<r;++i){
redunt[i] = redundant[i];
reverse(redunt[i].begin(),redunt[i].end());
}
if(parity=='e'){
for(i=0;i<r;++i){
base = redundant[i].length();
int count=0;
for(j=0;j<base;++j){
if(redundant[i][j]=='1')
++count;
}
if(count%2)
redundant[i]='1';
else
redundant[i]='0';
}
}else {
for(i=0;i<r;++i){
base = redundant[i].length();
int count=0;
for(j=0;j<base;++j){
if(redundant[i][j]=='1')
++count;
}
if(count%2)
redundant[i]='0';
else
redundant[i]='1';
}
}
j=0;
string hammesg="";
for(i=0;i<m+r;++i){
if(midmsg[i]=='2'){
midmsg[i] = 'R';
hammesg+=redundant[j];
++j;
}
else{
hammesg+=midmsg[i];
}
}
reverse(midmsg.begin(),midmsg.end());
reverse(hammesg.begin(),hammesg.end());
cout<<"\n";
cout<<"Bit calculation: "<<endl;
cout<<midmsg<<endl;
cout<<"\n";
for(i=0;i<r;++i){
cout<<"R"<<pow(2,i)<<" = "<<redunt[i]<<" = "<<redundant[i]<<endl;
}
cout<<"\n";
cout<<"Hamming Code: "<<hammesg<<endl;
cout<<"\n";
return make_tuple(hammesg,r);
}
void Reciver(string mesg,int r,char parity){
reverse(mesg.begin(),mesg.end());
int l=mesg.length();
int i=0,j,base=0;
string decode="";
string bits[r];
for(j=0;j<r;++j){
int temp = pow(2,j);
for(i=temp-1;i<l;i=i+temp){
int k = temp+1;
while(--k&&i<l){
bits[j]+=mesg[i];
++i;
}
}
}
if(parity=='e'){
for(i=0;i<r;++i){
base = bits[i].length();
int count=0;
for(j=0;j<base;++j){
if(bits[i][j]=='1')
++count;
}
if(count%2)
bits[i]='1';
else
bits[i]='0';
}
}else {
for(i=0;i<r;++i){
base = bits[i].length();
int count=0;
for(j=0;j<base;++j){
if(bits[i][j]=='1')
++count;
}
if(count%2)
bits[i]='0';
else
bits[i]='1';
}
}
for(int i=0;i<r;++i){
decode+=bits[i];
}
reverse(decode.begin(),decode.end());
cout<<"Reciver Side decoding: ";
cout<<decode<<endl;
}