-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp2-CRC.cpp
55 lines (47 loc) · 1.26 KB
/
Exp2-CRC.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
// Implement Cyclic redundancy check
#include <iostream>
using namespace std;
string XOR(string a, string b)
{
string ans = "";
for (int i = 0; i < a.length(); i++)
(a[i] == b[i]) ? (ans += '0') : ans += '1';
return ans;
}
string mod2Div(string dividend, string divisor)
{
int n = dividend.length();
int pos = divisor.length();
string temp = dividend.substr(0, pos);
while (pos <= n)
{
if (temp[0] == '1')
temp = XOR(temp, divisor).substr(1) + dividend[pos];
else
temp = temp.substr(1) + dividend[pos];
pos++;
}
return temp;
}
string CRC(string dataword, string divisor, int n, int k)
{
string augDataword = dataword + string(n - k, '0');
string remainder = mod2Div(augDataword, divisor);
string codeword = dataword + remainder;
return codeword;
}
int main()
{
int n, k;
cout << "Enter n: ";
cin >> n;
cout << "Enter k: ";
cin >> k;
string dataword, divisor;
cout << "Enter dataword of size " << k << " in the form of string: ";
cin >> dataword;
cout << "Enter divisor of size " << n - k + 1 << " in the form of string: ";
cin >> divisor;
string codeword = CRC(dataword, divisor, n, k);
cout << "The codeword produced is: " << codeword << endl;
}