 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Important question

1. Member Join Date
Mar 2018
Posts
40

## Important question

hi
Last edited by Kmilano; April 23rd, 2018 at 08:43 AM.  Reply With Quote

2. ## Re: Important question

What I gather from your code is that syndrome bits make the index in decodedBits minus 1.
Code:
```0 0 1 = 1, 1 -1 = 0
0 1 0 = 2, 2 -1 = 1
0 1 1 = 3, 3 -1 = 2
etc.```  Reply With Quote

3. Member Join Date
Mar 2018
Posts
40

## Re: Important question

for example if a vector decodedBits=[1 1 0 1 0 1 1]
then we will have : syndrome= [1 1 0 1 0 1 1]* check matrix(which I indicated above in my first post)
then in the part where if else began, I'm evaluating that if the result of syndrome is:
[0 0 1] it means syndrome=0, syndrome=0, syndrome=1 then according to binary table  is 1(in decimal), so the first bit of vector decodedBits has an error
[0 1 0]it means syndrome=0, syndrome=1, syndrome=0 then according to binary table  is 2(in decimal), so the second bit of vector decodedBits has an error
.... etc....

I need a way to implement my code when I extend my matrixes, because when matrixes extend for other classes, using if else in this code will be so long and I must find a more efficient way  Reply With Quote

4. ## Re: Important question

I just gave you a hint how you could avoid using if-else construct. Whether or how you will use the hint, it's totally up to you.

All that your if-else cases do is finding right index in decodedBits where correction needed. The index can be calculated directly, and the logic of calculation was explained in my first message. And please stop repeating "I need" and "I must". Just skip that and get directly to programming.

Another hint: you need a function that calculates index by the content of syndrome.
Last edited by Igor Vartanov; April 3rd, 2018 at 05:23 PM.  Reply With Quote

5. ## Re: Important question

As Ignor indicates in post #4, a function is needed that calculates the required index. syndrome is effectively a binary representation of the required index with syndrome being the MSB. So the function needs to convert a binary number into decimal. In the code below, this is the purpose of the lambda todec(). Consider (only partly tested)

Code:
```void D(const std::vector<double> &codedVector, std::vector<int> &decodedVector)
{
const auto todec = [](const std::string& st) {
long long unsigned int dec = 0;

for (const auto& d : st)
dec = (dec << 1) | d;

return dec;
};

const size_t maxbits = 7;
const size_t maxrows = 3;

int decodedBits[maxbits] = {0};

for (size_t bits = 0, mbit = std::min(maxbits, codedVector.size()); bits < mbit; ++bits)
decodedBits[bits] = (codedVector[bits] >= (voltage / 4));

std::string syndrome (maxrows, 0);

for (size_t a = 0; a < maxrows; ++a) {
char result = 0;

for (size_t b = 0; b < maxbits; ++b)
result += static_cast<char>(check[a][b] * decodedBits[b]);

syndrome[a] = result % 2;
}

if (auto b = todec(syndrome); (b > 0) && (b <= maxbits))
decodedBits[b - 1] ^= 1;
}```
Last edited by 2kaud; April 4th, 2018 at 05:59 AM.  Reply With Quote

6. Member Join Date
Mar 2018
Posts
40

## Re: Important question

Hello
referred to my question above (#1) and according to the whole definition and algorithm that I explained, in your opinion, the following piece of code is correct or not?

Code:
```unsigned n = 0;
int nrows = 15;
for (int i = 0; i < nrows; i++) {
n = (n << 1) | syndrome[i];
if (n != (1 << nrows) - 1)
decodedBits[n] ^= 1;```
In fact, this piece of code must be replaced instead of whole part of nested if-else statements in #1 and other parts are still fixed like before
Last edited by Kmilano; April 17th, 2018 at 11:49 AM.  Reply With Quote

7. ## Re: Important question

this piece of code must be replaced instead of whole part of nested if-else statements in #1 and other parts are still fixed like before
I'm not understanding -   Reply With Quote

8. Member Join Date
Mar 2018
Posts
40

## Re: Important question

the following code is the first code which I posted above:

Code:
```void D(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
int decodedBits;
for (int bits = 0; bits < 7; bits++) {
if (codedVector[bits] < (voltage / 4)) { decodedBits[bits] = 0; }
else { decodedBits[bits] = 1; }
}
char syndrome;
for (int a = 0; a < 3; a++) {
char result = 0;
for (int b = 0; b < 7; b++) {
result += (check[a][b] * decodedBits[b]);
}
syndrome[a] = result % 2;
}

if ((syndrome != 0) || (syndrome != 0) || (syndrome != 0)) {
//The syndrome indicates where the error is
if ((syndrome == 0) && (syndrome == 0) && (syndrome == 1)) {

decodedBits ^= 1;
}
else if ((syndrome == 0) && (syndrome == 1) && (syndrome == 0)) {

decodedBits ^= 1;
}
else if ((syndrome == 0) && (syndrome == 1) && (syndrome == 1)) {

decodedBits ^= 1;
}
else if ((syndrome == 1) && (syndrome == 0) && (syndrome == 0)) {

decodedBits ^= 1;
}
else if ((syndrome == 1) && (syndrome == 0) && (syndrome == 1)) {

decodedBits ^= 1;
}
else if ((syndrome == 1) && (syndrome == 1) && (syndrome == 0)) {

decodedBits ^= 1;
}
else {

decodedBits ^= 1;
}
}```

instead of nested if-else I added this piece of code:

Code:
```unsigned n = 0;
int nrows = 7;
for (int i = 0; i < nrows; i++) {
n = (n << 1) | syndrome[i];
if (n != (1 << nrows) - 1)
decodedBits[n] ^= 1;
}```
So if I replace it in the first code instead of that if-else series, I will have the following code:

Code:
```void D(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
int decodedBits;
for (int bits = 0; bits < 7; bits++) {
if (codedVector[bits] < (voltage / 4)) { decodedBits[bits] = 0; }
else { decodedBits[bits] = 1; }
}
char syndrome;
for (int a = 0; a < 3; a++) {
char result = 0;
for (int b = 0; b < 7; b++) {
result += (check[a][b] * decodedBits[b]);
}
syndrome[a] = result % 2;
}

if ((syndrome != 0) || (syndrome != 0) || (syndrome != 0)) {
unsigned n = 0;
int nrows = 7;
for (int i = 0; i < nrows; i++) {
n = (n << 1) | syndrome[i];
if (n != (1 << nrows) - 1)
decodedBits[n] ^= 1;
}
}
}```
Is it correct or not?, I ask it because I cannot run this part in my project, because I must do framework and then I can compile this part.  Reply With Quote

9. ## Re: Important question

function D() compiles OK for me using VS2017 (15.6.6). When you are in a position to compile and test the code yourself then you'll be able to use the debugger and check that it is providing the required result.  Reply With Quote

#### Tags for this Thread

c++, c++ array 2-d #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• Click Here to Expand Forum to Full Width