-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamming.c
230 lines (197 loc) · 4.37 KB
/
hamming.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "hamming.h"
void printArray(uint32_t arr[], uint32_t size){
printf("\n");
for(uint32_t i = 0; i < size; i++){
printf("%d\t", arr[i]);
}
}
uint32_t hammingLength(uint32_t size){
/*
Returns The total length of the encoded message.
Formula : No. of Data Bits + No. of Redundant Bits + 1 (Parit Bit)
*/
uint32_t i = 0;
uint32_t p = 1;
while(p <= i+size){
p<<=1;
i++;
}
return size + i + 1;
}
uint32_t redundantBitValues(uint32_t *n, uint32_t redCount, uint32_t pBit ){
/*
Returns The Redundant Bit value for given Bit Location (Power of 2)
*/
uint32_t res = 0;
uint32_t i = 0;
while (i < redCount){
// X-ORs every bit whose location has binary up for pBit.
if ((i+1) & pBit && i+1 != pBit){
res ^= n[i];
}
i++;
}
return res;
}
uint32_t *encodeHammer(uint32_t a[], uint32_t size, uint32_t redCount){
/*
Returns An Encoded Hamming code array of given data bits.
*/
uint32_t *n = (uint32_t *) malloc(sizeof(int) * redCount);
uint32_t i = 2;
uint32_t s = 0;
uint32_t ptrack = 4;
uint32_t pBits = 0;
// 1. Assigning data bits to non-2-power locations in the resultant array.
while (s < size){
if (i+1 == ptrack){
ptrack<<=1;
i++;
}
n[i] = a[s];
pBits ^= n[i];
s++;
i++;
}
// 2. Adding The Parity Bit Values to all 2-power locations.
i = 0;
ptrack = 1;
while(ptrack <= i+size){
n[ptrack - 1] = redundantBitValues(n, redCount - 1, ptrack);
pBits ^= n[ptrack - 1];
ptrack<<=1;
i++;
}
// 3. Last Bit, Parity bit value assignment.
n[redCount - 1] = pBits;
return n;
}
int *encodeParity(int a[], int size, int redCount){
int *n = (int *) malloc(sizeof(int) * redCount);
int i = 0;
int s = 0;
int ptrack = 1;
while (s < size){
if (i+1 == ptrack){
ptrack<<=1;
i++;
continue;
}
n[i] = a[s];
s++;
i++;
}
n[++i] = 0;
i = 0;
ptrack = 1;
while(ptrack <= i+size){
n[i] = redundantBitValues(n, redCount, size, ptrack);
ptrack<<=1;
i++;
}
return n;
}
uint32_t parityChecker(uint32_t n[] ,uint32_t redCount){
uint32_t ptrack = 1;
uint32_t pos = 0;
// XOR of Parity Bit and Second Last Data Bit
uint32_t parity = n[redCount - 2] ^ n[redCount - 1];
// XOR of All Bits in the Encoded Array
parity ^= redundantBitValues(n, redCount , redCount - 1);
while (ptrack < redCount){
// Comparing Parity Bit Values passed vs Parity Bit Values Calculated.
if (n[ptrack-1] ^ redundantBitValues(n, redCount, ptrack)){
pos += ptrack;
}
ptrack<<=1;
}
printf("\n\nparity bit: %d\tposition: %d\n", parity, pos);
// Correcting / Flipping the Possible Error Bits
if (pos > 0 && parity == 1){
n[pos-1] ^= 1;
printf("Single Error Detected and corrected!");
return 1;
} else if (pos > 0 && parity == 0){
printf("Double Error Detected!");
return 3;
}
else{
printf("No Error Detected!");
return 0;
}
}
int parityChecker_arrays(int n[] ,int redCount, int p[], int size){
int i = 0;
int s = 1;
int ptrack = 1;
int size = 1;
int red = 0;
int t = 0;
int res = 1;
int p = 1;
while(ptrack<<1 <= redCount ){
size++;
ptrack <<= 1;
}
ptrack = 1;
while (s < size+1){
if (i+1 == ptrack){
red = redundantBitValues(n, redCount, redCount, ptrack);
t = t + (res * (n[i] ^ red));
res *= 10;
ptrack<<=1;
i++;
p ^= n[i];
continue;
}
p ^= n[i];
s++;
i++;
}
res = red = i = 0;
while (t!=0){
red = t%10;
t /= 10;
res += red*(1<<i);
i++;
}
if (res > 0 && p > 0){
n[res-1] ^= 1;
return 3;
} else if(p > 0 && res == 0){
return 2;
} else if(res > 0){
return 1;
} else{
return 0;
}
}
int *decoder(int *a, int redCount){
uint32_t i = 2;
uint32_t s = 0;
uint32_t ptrack = 1;
uint32_t size = 1;
// Calculating the length of data bits
while(ptrack<<1 <= redCount ){
size++;
ptrack <<= 1;
}
// Creating an Array for decoded msg
uint32_t *n = (uint32_t *) malloc(sizeof(int) * size );
ptrack = 4;
// Getting all the data bits which are located in non-2-power locations.
while (i < redCount - 1){
if (i+1 & ptrack){
ptrack<<=1;
i++;
continue;
}
n[s] = a[i];
s++;
i++;
}
return n;
}