#include <iostream>
using namespace std;
void calculateCRC(string data, string divisor) {
int dataLength = data.length();
int divisorLength = divisor.length();
// Append zeros to the data (same as the length of the divisor - 1)
string dividend = data;
for (int i = 1; i < divisorLength; i++) {
dividend += "0";
}
// Perform division using XOR
for (int i = 0; i <= dividend.length() - divisorLength; i++) {
if (dividend[i] == '1') {
for (int j = 0; j < divisorLength; j++) {
dividend[i + j] = (dividend[i + j] == divisor[j]) ? '0' : '1';
}
}
}
// Extract the remainder (last divisorLength-1 bits)
string crc = dividend.substr(dataLength, divisorLength - 1);
// Append CRC to the original data
string transmittedData = data + crc;
// Output
cout << "Original Data: " << data << endl;
cout << "CRC: " << crc << endl;
cout << "Transmitted Data: " << transmittedData << endl;
}
int main() {
string data, divisor;
cout << "Enter the binary data: ";
cin >> data;
cout << "Enter the binary divisor: ";
cin >> divisor;
calculateCRC(data, divisor);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBjYWxjdWxhdGVDUkMoc3RyaW5nIGRhdGEsIHN0cmluZyBkaXZpc29yKSB7CiAgICBpbnQgZGF0YUxlbmd0aCA9IGRhdGEubGVuZ3RoKCk7CiAgICBpbnQgZGl2aXNvckxlbmd0aCA9IGRpdmlzb3IubGVuZ3RoKCk7CgogICAgLy8gQXBwZW5kIHplcm9zIHRvIHRoZSBkYXRhIChzYW1lIGFzIHRoZSBsZW5ndGggb2YgdGhlIGRpdmlzb3IgLSAxKQogICAgc3RyaW5nIGRpdmlkZW5kID0gZGF0YTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgZGl2aXNvckxlbmd0aDsgaSsrKSB7CiAgICAgICAgZGl2aWRlbmQgKz0gIjAiOwogICAgfQoKICAgIC8vIFBlcmZvcm0gZGl2aXNpb24gdXNpbmcgWE9SCiAgICBmb3IgKGludCBpID0gMDsgaSA8PSBkaXZpZGVuZC5sZW5ndGgoKSAtIGRpdmlzb3JMZW5ndGg7IGkrKykgew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