fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void calculateCRC(string data, string divisor) {
  5. int dataLength = data.length();
  6. int divisorLength = divisor.length();
  7.  
  8. // Append zeros to the data (same as the length of the divisor - 1)
  9. string dividend = data;
  10. for (int i = 1; i < divisorLength; i++) {
  11. dividend += "0";
  12. }
  13.  
  14. // Perform division using XOR
  15. for (int i = 0; i <= dividend.length() - divisorLength; i++) {
  16. if (dividend[i] == '1') {
  17. for (int j = 0; j < divisorLength; j++) {
  18. dividend[i + j] = (dividend[i + j] == divisor[j]) ? '0' : '1';
  19. }
  20. }
  21. }
  22.  
  23. // Extract the remainder (last divisorLength-1 bits)
  24. string crc = dividend.substr(dataLength, divisorLength - 1);
  25.  
  26. // Append CRC to the original data
  27. string transmittedData = data + crc;
  28.  
  29. // Output
  30. cout << "Original Data: " << data << endl;
  31. cout << "CRC: " << crc << endl;
  32. cout << "Transmitted Data: " << transmittedData << endl;
  33. }
  34.  
  35. int main() {
  36. string data, divisor;
  37.  
  38. cout << "Enter the binary data: ";
  39. cin >> data;
  40.  
  41. cout << "Enter the binary divisor: ";
  42. cin >> divisor;
  43.  
  44. calculateCRC(data, divisor);
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5284KB
stdin
 
stdout
Enter the binary data: Enter the binary divisor: Original Data: 
CRC: 
Transmitted Data: