fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. // Max size for data and polynomial
  5. #define MAX 100
  6.  
  7. char t[MAX], cs[MAX], g[] = "1011";
  8. int a, e, c;
  9.  
  10. // Function to perform XOR operation
  11. void xor_func() {
  12. int N = strlen(g);
  13. for (c = 1; c < N; c++) {
  14. cs[c] = ((cs[c] == g[c]) ? '0' : '1');
  15. }
  16. }
  17.  
  18. // Function to calculate CRC/Remainder
  19. void crc_calc() {
  20. int N = strlen(g);
  21. // Initialize cs with the first N bits of t
  22. for (e = 0; e < N; e++) {
  23. cs[e] = t[e];
  24. }
  25.  
  26. int next_bit = N;
  27. do {
  28. if (cs[0] == '1') {
  29. xor_func();
  30. }
  31.  
  32. // Shift bits left by 1
  33. for (c = 0; c < N - 1; c++) {
  34. cs[c] = cs[c + 1];
  35. }
  36. // Pull the next bit from the data
  37. cs[c] = t[next_bit++];
  38.  
  39. } while (next_bit <= a + N - 1);
  40. }
  41.  
  42. int main() {
  43. // Clear arrays
  44. memset(t, 0, MAX);
  45. memset(cs, 0, MAX);
  46.  
  47. printf("Enter data bits: ");
  48. scanf("%s", t);
  49.  
  50. a = strlen(t); // Original data length
  51. int N = strlen(g); // Polynomial length
  52.  
  53. printf("\nGenerating polynomial: %s", g);
  54.  
  55. // Append N-1 zeros to the data (Padding)
  56. for (e = a; e < a + N - 1; e++) {
  57. t[e] = '0';
  58. }
  59. t[e] = '\0';
  60.  
  61. printf("\nData after padding: %s", t);
  62.  
  63. crc_calc(); // Calculate Checksum
  64.  
  65. printf("\nCalculated Checksum: %s", cs);
  66.  
  67. // Replace the padding bits with the actual checksum bits
  68. for (e = a; e < a + N - 1; e++) {
  69. t[e] = cs[e - a];
  70. }
  71.  
  72. printf("\nFinal Codeword to be sent: %s", t);
  73. printf("\n---------------------------------------");
  74.  
  75. // Error simulation
  76. printf("\nTest error detection? (1 for Yes, 0 for No): ");
  77. int choice;
  78. scanf("%d", &choice);
  79.  
  80. if (choice == 1) {
  81. printf("Enter bit position to flip (1 to %d): ", (int)strlen(t));
  82. int pos;
  83. scanf("%d", &pos);
  84. if(pos > 0 && pos <= (int)strlen(t)) {
  85. t[pos - 1] = (t[pos - 1] == '0') ? '1' : '0';
  86. printf("Data with error: %s", t);
  87. }
  88. }
  89.  
  90. crc_calc(); // Recalculate to verify
  91.  
  92. // Check if remainder (cs) is all zeros
  93. int error_found = 0;
  94. for (e = 0; e < N - 1; e++) {
  95. if (cs[e] == '1') {
  96. error_found = 1;
  97. break;
  98. }
  99. }
  100.  
  101. printf("\n---------------------------------------");
  102. if (error_found) {
  103. printf("\nRESULT: Error detected (Remainder is not 0)");
  104. } else {
  105. printf("\nRESULT: No error detected (Data is clean)");
  106. }
  107. printf("\n---------------------------------------\n");
  108.  
  109. return 0;
  110. }
Success #stdin #stdout 0s 5304KB
stdin
45
stdout
Enter data bits: 
Generating polynomial: 1011
Data after padding: 45000
Calculated Checksum: 000
Final Codeword to be sent: 45000
---------------------------------------
Test error detection? (1 for Yes, 0 for No): 
---------------------------------------
RESULT: No error detected (Data is clean)
---------------------------------------