-
Notifications
You must be signed in to change notification settings - Fork 37
/
HammingCode.cpp
91 lines (88 loc) · 1.83 KB
/
HammingCode.cpp
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
#include<iostream>
#include<math.h>
using namespace std;
int transmit[100];
int calculateRedudantBits(int m)
{
int i;
i = 0;
while(pow(2,i) < m + i + 1) /*2^r ≥ m + r + 1, where, r = redundant bit, m = data bit*/
{
i = i+1;
}
return i;
}
void appendData(int m, int data[], int length)
{
int total = m + length;
int j =0;
int k =length-1;
for(int i =0; i<total;i++)
{
if(i == (pow(2,j) - 1)) /*(Powers of 2 - 1,2,4,8,16)*/
{
j = j+1;
transmit[i] = 2;
}
else
{
transmit[i] = data[k];
k = k-1;
}
}
}
int calculateParity(int r,int m, int length)
{ //rth position
int index;
int sum = 0;
for(int i =0; i <m+length;i++)
{
index = i+1;
for(int j = 0; j<r; j++)
{
index = index >> 1;
}
if(index&1==1)
{
sum = sum + transmit[i];
}
}
if(sum%2 == 0)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
int length;
cout << "Enter the length of data to be transmitted:" ;
cin >> length;
int data[length];
cout << "Enter the data to be transmitted:" ;
for(int i =0; i<length;i++)
{
cin >> data[i];
}
int m = calculateRedudantBits(length);
appendData(m,data,length);
for(int i =m+length-1; i>=0;i--)
{
cout << transmit[i] << " ";
}
cout << endl;
for(int i=0;i<m;i++)
{
int random = (pow(2,i) - 1);
transmit[random] = calculateParity(i,m,length);
}
cout << "Transmitted Data is: ";
for(int i =m+length-1; i>=0;i--)
{
cout << transmit[i] << " ";
}
return 0;
}