Function crypto::hamming::decode

source ·
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. $p_1$ is calculated by the XOR of itself (e[0]), $d_1$ (e[2]), $d_2$ (e[4]), and $d_4$ (e[6]). We XOR these values back together to recover $p_1$. 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 $p_1$ is placed as the first bit, $p_2$ as the second bit, and $p_3$ as the third bit to denote the position of the error.

If only $p_1$ was flipped, it would be 1, and the bit string denoted by $p_1$, $p_2$ and $p_3$ would be 001, which denotes that the first bit ($p_1$) was flipped.