-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamming.c
More file actions
290 lines (230 loc) · 5.19 KB
/
hamming.c
File metadata and controls
290 lines (230 loc) · 5.19 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Program to generate the hamming code for the message at the sender end, introduce error manually(via user input) and check it at the receiver
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#define MAX 100
void hc_gen(int data[], int m)
{
int hamming[MAX] = {0};
int r, h;
int i, j, k;
int p;
int parity;
r = 0;
/*Calculating the number of parity bits, r needed for the code
r - number of parity bits*/
while((m + r + 1)>(pow(2,r)))
r++;
printf("\nNumber of redundant bits needed is: %d", r);
/*Set all the parity locations to -1
and add the msg bits in the appropriate location*/
// h - length of the hamming code with redundant bits
h = r + m;
// exponent for 2
j = 0;
// index for data
k = 0;
for(i=1;i<=h;i++)
{
p = pow(2,j);
if(p == i)
{
hamming[i] = -1;
j++;
}
else
{
hamming[i] = data[k];
k++;
}
}
/*
//DEBUGGING
printf("\nh:%d", h);
for(i=1;i<=h;i++)
printf("\ndata%d: %d", i, hamming[i]);
printf("\n");
*/
/*Set the EVEN parity values for the redundant bits*/
k = 0;
int x, min, max = 0;
for (i = 1; i <= h; i = pow (2, k))
{
k++;
parity = 0;
j = i;
//x = i;
min = 1;
max = i;
while(j <= h)
{
for(x = j; max >= min && x <= h; min++, x++)
{
if (hamming[x] == 1)
parity = parity + 1;
}
j = x + i;
min = 1;
}
//checking for even parity
if (parity % 2 == 0)
hamming[i] = 0;
else
hamming[i] = 1;
}
/*Need to display in reverse */
printf("\nData with redundant bits(Hamming Code): ");
for(i=h; i>=1; i--)
printf("%d", hamming[i]);
printf("\n");
}
void hc_err_detect(int hamming[], int h)
{
int data[MAX] = {0};
int r, m;
int i, j, k;
int parity;
int error[MAX];
int msg[MAX];
r = 0;
/*Calculating the number of parity bits, r needed for the code
r - number of parity bits*/
while((h + 1)>(pow(2,r)))
r++;
/*Set all the parity locations to -1
and add the msg bits in the appropriate location*/
// h - length of the data code with redundant bits
m = h - r;
/*
//DEBUGGING
printf("\nh:%d m:%d r:%d", h, m, r);
*/
int index = 1;
/*Find the EVEN parity values for the redundant bits*/
k = 0;
int x, min, max = 0;
for (i = 1; i <= h; i = pow (2, k))
{
k++;
parity = 0;
j = i;
//x = i;
min = 1;
max = i;
while(j <= h)
{
for(x = j; max >= min && x <= h; min++, x++)
{
if (hamming[x] == 1)
parity = parity + 1;
}
j = x + i;
min = 1;
}
//checking for even parity
if (parity % 2 == 0)
data[index++] = 0;
else
data[index++] = 1;
}
/*Need to reverse*/
for(i=r, j=1; i>=1; i--, j++)
error[j] = data[i];
printf("\nCalulated redundant bits(error pos): ");
for(i=1; i<=r;i++)
printf("%d", error[i]);
printf("\n");
int c = r-1;
int err_pos = 0;
/*Converting the binary value in the error int array into the equivalent decimal index*/
for(i=1; i<=r; i++)
err_pos += (error[i] * pow(2, c--));
/*DEBUG
printf("\nError position: %d", err_pos);
*/
// 0 index means no error since all indexing in this program starts from 1 to match the indexing for hamming code which starts with 1
if(err_pos != 0)
{
hamming[err_pos] = hamming[err_pos] == 0 ?1:0; // changing the 0 to 1 and vice-versa
//printf("\nCorrected data: ");
}
/*
else
printf("Data: ");
for(i=h; i>=1; i--)
printf("%d", hamming[i]);
printf("\n");
*/
/*Setting the redundant bit positions to -1*/
int p;
j = 0;
for(i=1;i<=h;i++)
{
p = pow(2,j);
if(p == i)
{
hamming[i] = -1;
j++;
}
}
/*DEBUG
printf("-1 Data: ");
for(i=h; i>=1; i--)
printf("%d", hamming[i]);
printf("\n");
*/
/*Retrieving the message from the hamming code without the redundant bits*/
index = 1;
for(i=1; i<=h; i++)
{
if (hamming[i] != -1)
msg[index++] = hamming[i];
}
printf("Corrected Data: ");
for(i=m; i>=1; i--)
printf("%d", msg[i]);
printf("\n");
}
int main()
{
char msg[MAX];
int data[MAX];
int m;
int i;
int j;
/*Reading the message or data to be sent*/
printf("Input data: ");
scanf("%s", msg);
m = strlen(msg);
/*Reversing the msg read
arr indexing: 0 to len - 1
msg bit read has indexing: len-1 to 0
msb should be in array index len_of_array - 1 but will be in index 0
To correct this, store the msg in data in the reversed order*/
for(i=m-1, j=0; i>=0; i--, j++)
data[i] = msg[j] - '0';
/*
//DEBUGGING
printf("\nm:%d", m);
for(i=0;i<m;i++)
printf("\ndata%d: %d", i+1,data[i]);
printf("\n");
*/
/*Calling the hamming code generator helper function
to obtain the code sent by the Sender*/
hc_gen(data, m);
printf("Introduce error in data: ");
scanf("%s", msg);
m = strlen(msg);
/*Reversing the msg read
arr indexing: 0 to len - 1
msg bit read has indexing: len-1 to 0
msb should be in array index len_of_array - 1 but will be in index 0
To correct this, store the msg in data in the reversed order*/
for(i=m, j=0; i>=1; i--, j++)
data[i] = msg[j] - '0';
hc_err_detect(data, m);
return 0;
}