Guest User

Untitled

a guest
Feb 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned char byte;
  9.  
  10. int popcount(byte b)
  11. {
  12. int count = 0;
  13. for(int i = 0; i < 8; i++)
  14. {
  15. if(b & (1 << i))
  16. count++;
  17. }
  18. return count;
  19. }
  20.  
  21. byte mult(vector<byte>& A, byte w)
  22. {
  23. byte syndrome = 0;
  24. for(size_t i = 0; i < A.size(); i++)
  25. {
  26. //check parity of row i
  27. bool parity = popcount(A[i] & w) % 2 == 1;
  28. if(parity)
  29. {
  30. syndrome |= (1 << i);
  31. }
  32. }
  33. return syndrome;
  34. }
  35.  
  36. void printBin(byte b)
  37. {
  38. for(int i = 7; i >= 0; i--)
  39. cout << ((b & (1 << i)) ? '1' : '0');
  40. }
  41.  
  42. int main()
  43. {
  44. vector<byte> A;
  45. A.push_back(0x1E);
  46. A.push_back(0x66);
  47. A.push_back(0xAA);
  48. A.push_back(0xFF);
  49. cout << "Code matrix:\n";
  50. for(int i = 0; i < 4; i++)
  51. {
  52. printBin(A[i]);
  53. cout << '\n';
  54. }
  55. cout << '\n';
  56. cout << "All codewords:\n";
  57. int numCodewords = 0;
  58. for(int i = 0; i < 256; i++)
  59. {
  60. byte b = i;
  61. if(mult(A, b) == 0)
  62. {
  63. printBin(b);
  64. cout << '\n';
  65. numCodewords++;
  66. }
  67. }
  68. vector<set<byte>> cosets(16);
  69. cout << "Code has " << numCodewords << " codewords.\n";
  70. for(int i = 0; i < 256; i++)
  71. {
  72. byte syndrome = mult(A, i);
  73. cosets[syndrome].insert(i);
  74. }
  75. /*
  76. cout << "Cosets:\n";
  77. for(int i = 0; i < 16; i++)
  78. {
  79. for(auto w : cosets[i])
  80. {
  81. printBin(w);
  82. cout << ' ';
  83. }
  84. cout << '\n';
  85. }
  86. cout << '\n';
  87. */
  88. vector<byte> leaders(16);
  89. for(int i = 0; i < 16; i++)
  90. {
  91. byte leader = 0xFF;
  92. byte minPop = 8;
  93. for(auto w : cosets[i])
  94. {
  95. if(popcount(w) < minPop)
  96. {
  97. minPop = popcount(w);
  98. leader = w;
  99. }
  100. }
  101. cout << "Syndrome ";
  102. printBin(i);
  103. cout << " leader: ";
  104. printBin(leader);
  105. cout << '\n';
  106. leaders[i] = leader;
  107. }
  108. vector<byte> toDecode;
  109. toDecode.push_back(0xAD);
  110. toDecode.push_back(0x5B);
  111. toDecode.push_back(0xC0);
  112. for(auto w : toDecode)
  113. {
  114. cout << "Decoding ";
  115. printBin(w);
  116. cout << " ...\n";
  117. byte syndrome = mult(A, w);
  118. cout << "Syndrome is ";
  119. printBin(syndrome);
  120. cout << "\n";
  121. cout << "Corrected word: ";
  122. printBin(w ^ leaders[syndrome]);
  123. cout << "\n\n";
  124. }
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment