pub fn decode(e: [bool; 7]) -> Either<[bool; 4], (usize, [bool; 4])>
Expand description
We want to recalculate the parity bits. If all of them are 0, then there was no error in transmission. If any of them are non-zero, we know there’s an error. Since 3 bits can express up to 8 states, we count the first parity bit as the 1st bit, the second bit as the second, and the third as the last bit. Then, we use that to correct the error, wherever it was transmitted, and then return the data, along with the error position, if it was found.
The decoding process reverse the encoding process to recover the parity bits and then use them in its implementation. is calculated by the XOR of itself (e[0]), (e[2]), (e[4]), and (e[6]). We XOR these values back together to recover . If any of the values were flipped, then the result will be non-zero.
The same thing is repeated for the other parity bits, and finally, the is placed as the first bit, as the second bit, and as the third bit to denote the position of the error.
If only was flipped, it would be 1, and the bit string denoted by , and would be 001, which denotes that the first bit () was flipped.