□ Communications · parity and XOR
Hamming(7,4) error correction
Encode four data bits with three parity bits, then find and fix a single flipped bit.
- Level 4 · Systems Engineer
- 6 tests
- technically checked
Your task
Radiation or electrical noise can flip a bit in memory or on a link. Hamming(7,4) adds three parity bits to four data bits so the receiver can locate and correct any single flipped bit.
Bits are numbered 1–7 in the order [p1, p2, d1, p3, d2, d3, d4]:
- p1 = d1 ^ d2 ^ d4, p2 = d1 ^ d3 ^ d4, p3 = d2 ^ d3 ^ d4
Write:
- encode(data): data = [d1, d2, d3, d4] → the 7-bit array.
- decode(bits): compute s1 = b1^b3^b5^b7, s2 = b2^b3^b6^b7, s3 = b4^b5^b6^b7. The error position is s1 + 2·s2 + 4·s3 (0 means no error). Flip that bit if needed, then return [d1, d2, d3, d4].
Hints (0 of 3 shown)
What gets checked
- encode([1, 0, 1, 1]) → [0, 1, 1, 0, 0, 1, 1]
- encode([0, 0, 0, 0]) → [0, 0, 0, 0, 0, 0, 0]
- decode([0, 1, 1, 0, 0, 1, 1]) → [1, 0, 1, 1]
Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.