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
|
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
//=====================================================================
string toBinary( int n, int p )
{
string result;
for ( ; p; p--, n /= 2 ) result = (char)( '0' + n % 2 ) + result;
return result;
}
//=====================================================================
// Define a mapping from syndomeNumber to the decodedBit index for pBits parity bits
//
// Ordering in decodedBit[] is: D(s-1) D(s-2) ... D(0) P(p-1) P(p-2) ... P(0)
// where D is a data bit, P is a parity bit
//
map<int,int> setMap( int pBits )
{
int N = ( 1 << pBits ) - 1; // Maximum number representable (2^p-1)
map<int,int> M;
// Set up mapping of parity bits (2^i)
int n = 1;
for ( int i = 0; i < pBits; i++ )
{
M[n] = N - 1 - i;
n *= 2;
}
// Remaining data bits
int next = 0;
for ( int i = N; i >= 1; i-- )
{
if ( M.count( i ) == 0 ) // If NOT a parity bit
{
M[i] = next;
next++;
}
}
// Check if required
cout << "Outcome of mapping: syndrome and index:\n";
for ( auto e : M ) cout << toBinary( e.first, pBits ) << " " << e.second << '\n';
return M;
}
//=====================================================================
int getSyndromeNumber( vector<int> syndrome )
{
int result = 0;
for ( int i = 0; i < syndrome.size(); i++ ) result = 2 * result + syndrome[i];
return result;
}
//=====================================================================
int main()
{
const int parityBits = 3;
map<int,int> M = setMap( parityBits );
cout << "\n\n";
vector< vector<int> > tests = { { 1, 1, 1 }, { 1, 1, 0 }, { 1, 0, 1 }, { 0, 1, 1 },
{ 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
cout << "Tests:\n";
for ( auto v : tests )
{
for ( int i : v ) cout << i << ' ';
cout << " ---> decodedBit number " << M[ getSyndromeNumber( v ) ] << '\n';
}
}
|