error refactor

This commit is contained in:
Lewis Moten
2024-05-14 02:22:27 -04:00
parent 8cb5248fdf
commit 3aefa71390

View File

@@ -24,30 +24,40 @@ export const decode = bits => {
return decodedBits; return decodedBits;
} }
const encodeBlock = bits => { const encodeBlock = ([a = 0, b = 0, c = 0, d = 0]) => {
if(bits.length !== DECODED_SIZE) return []; // embed parity bits
return [ return [
bits[0] ^ bits[1] ^ bits[3], a ^ b ^ d,
bits[0] ^ bits[2] ^ bits[3], a ^ c ^ d,
bits[0], a,
bits[1] ^ bits[2] ^ bits[3], b ^ c ^ d,
bits[1], b,
bits[2], c,
bits[3] d
] ]
} }
const decodeBlock = bits => { const decodeBlock = ([
if(bits.length !== ENCODED_SIZE) return []; p0 = 0,
const error_1 = bits[0] ^ bits[2] ^ bits[4] ^ bits[6]; p1 = 0,
const error_2 = bits[1] ^ bits[2] ^ bits[5] ^ bits[6]; a = 0,
const error_3 = bits[3] ^ bits[4] ^ bits[5] ^ bits[6]; p2 = 0,
let error = (error_3 << 2) | (error_2 << 1) | error_1; b = 0,
if(error !== 0) bits[error - 1] ^= 1; c = 0,
return [ d = 0
bits[2], ]) => {
bits[4], // check parity bits
bits[5], const e0 = p0 ^ a ^ b ^ d;
bits[6] const e1 = p1 ^ a ^ c ^ d;
]; const e2 = p2 ^ b ^ c ^ d;
let error = (e2 << 2) | (e1 << 1) | e0;
// flip the bit
switch(error) {
case 0b011: a ^= 1; break;
case 0b101: b ^= 1; break;
case 0b110: c ^= 1; break;
case 0b111: d ^= 1; break;
default: break;
}
return [a, b, c, d];
} }