-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrc.cpp
More file actions
146 lines (118 loc) · 3.35 KB
/
Copy pathcrc.cpp
File metadata and controls
146 lines (118 loc) · 3.35 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
#define VERBOSE
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <string.h>
using namespace std;
class BitArray
{
vector<bool> *_data;
int size;
public:
BitArray(int size)
{
this->_data = new vector<bool>(size, 0);
this->size = size;
}
BitArray(const string &str)
{
auto size = str.length();
for (int i = 0; i < size; ++i)
if (str[i] != '1' && str[i] != '0')
throw "invalid character in bitstring";
this->size = str.length();
this->_data = new vector<bool>();
for (int i = 0; i < str.length(); ++i)
_data->push_back((str[i] == '1'));
}
BitArray(const BitArray &bitArray)
{
this->size = bitArray.size;
this->_data = new vector<bool>();
for (int i = 0; i < size; ++i)
_data->push_back(bitArray._data->at(i));
}
void Append(BitArray other)
{
for (int i = 0; i < other.size; ++i)
_data->push_back(other._data->at(i));
size += other.size;
}
BitArray Last(int bits)
{
if (bits > size)
throw "number of bits requested is larger than the size of the bitarray";
BitArray result(0);
result.size = bits;
for (int i = size - bits, j = 0; j < bits; ++i, ++j)
result._data->push_back(this->_data->at(i));
return result;
}
friend ostream &operator<<(ostream &os, const BitArray &b)
{
for (int i = 0; i < b.size; ++i)
os << (b._data->at(i) ? '1' : '0');
return os;
}
friend BitArray operator/(const BitArray ÷nd, const BitArray &divisor)
{
if (divisor.size > dividend.size)
throw "divided's length cannot be less than divisor's";
#ifdef VERBOSE
cout << "\n\nCALCULATE: "
<< dividend << " / " << divisor << endl;
#endif
BitArray num(dividend);
const BitArray empty(divisor.size);
#ifdef VERBOSE
cout << num << endl;
#endif
for (int i = 0; i <= (dividend.size - divisor.size); ++i)
{
auto div = (num._data->at(i) ? &divisor : &empty);
for (int j = 0; j < divisor.size; ++j)
num._data->at(i + j) = num._data->at(i + j) ^ (*div)._data->at(j);
#ifdef VERBOSE
for (int k = 0; k < i; ++k)
cout << ' ';
cout << (*div) << endl;
cout << num << endl;
#endif
}
#ifdef VERBOSE
cout << "\n\n";
#endif
return num.Last(divisor.size - 1);
}
std::string toString()
{
std::stringstream ss;
for (auto i = _data->begin(); i != _data->end(); ++i)
ss << (*i ? '1' : '0');
return ss.str();
}
};
class CRC_CCITT
{
public:
static BitArray generate(BitArray data)
{
BitArray divisor(string("10001000000100001"));
data.Append(BitArray(16));
return data / divisor;
}
};
int main()
{
string bits;
cout << "Enter the data bits: ";
cin >> bits;
auto data = BitArray(bits);
auto crc = CRC_CCITT::generate(data);
data.Append(crc);
cout << "CRC at sender: " << crc << endl;
cout << "Data to be sent: " << data << endl;
cout << "\nReceived data: " << data << endl;
cout << "CRC at receiver: " << CRC_CCITT::generate(BitArray(data)) << endl;
}