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. p1p_1 is calculated by the XOR of itself (e[0]), d1d_1 (e[2]), d2d_2 (e[4]), and d4d_4 (e[6]). We XOR these values back together to recover p1p_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 p1p_1 is placed as the first bit, p2p_2 as the second bit, and p3p_3 as the third bit to denote the position of the error.

If only p1p_1 was flipped, it would be 1, and the bit string denoted by p1p_1, p2p_2 and p3p_3 would be 001, which denotes that the first bit (p1p_1) was flipped.