c++hamming-code

for loop with nested if is broken, and transfers all 1's instead of data to an array


after several hours of testing i have narrowed down the problem to a single for loop, instead of the for loop (line 64) with the nested if transferring the data from converMain[] to converMain1[] all the data is set to 1's, Ive gone through and everything else works fine, its just this for loop.

int converMain[14];
converMain1[0] = 1;
int g = 1;
for (int i = 1; i < 19; i++) {
if (i == 1 || 2 || 4 || 8) { //tests if we are on a parody bit
converMain1[i] = p[i]; //puts parody bit in Main1 array
        g++; //this variable helps sync the bits in the               
                         //right spot in the Main1 array 
    }
    else {
    converMain1[i] = converMain[i - g];         
//puts binary stored in Main array into Main1 array with correct sync
    }
}

Output:

0001011 h 1010011 e 1001111101111111101 0010 392953 104 101 00010111010011
parody bits          ^^ ^   ^
0011011 l 0011011 l 1001111101111111101 0010 392953 108 108 00110110011011
parody bits          ^^ ^   ^
1111011 o 1110111 w 1101011101111111101 1000 392939 111 119 11110111110111
parody bits          ^^ ^   ^
1111011 o 0100111 r 1111011101111111101 1100 392943 111 114 11110110100111
parody bits          ^^ ^   ^
0011011 l 0010011 d 1111111111111111101 1111 393215 108 100 00110110010011
parody bits          ^^ ^   ^
1000010 ! 1000010 ! 1011011101111111101 0100 392941 33 33 10000101000010
parody bits          ^^ ^   ^ 

we should expect the number on the far right to be strung along into number here in the middle witht he arrows ignoring the arrows' positions since those are the parody bits and just shifting over them before putting the rest of the array down.


Solution

  • if (i == 1 || 2 || 4 || 8) is always true.

    if ((i == 1) || (2) || (4) || (8)) is how the compiler reads it, thus it stops at lest at 2 since it's true.

    A natural solution would be if (i == 1 || i == 2 || i == 4 || i == 8) ....