Hamming Code Simulation
Our team choose Google Colab as the development environment for
implementing the Hamming (7, 4) encoder and decoder. Colab’s cloud-based
notebooks let every member edit and run Python code directly in the browser,
eliminating local setup issues and ensuring real-time collaboration. The platform also
supplies free CPU and GPU resources, which sped up our experiments when
simulating bit-flip errors and testing the correction logic. As a result, we could keep
all code, explanations, and output in a single, shareable notebook that is ready for
demonstration and review. You can view the notebook here: Hamming code
Picture .1. encode_hamming_7_4 function
encode_hamming_7_4 function turns four data bits into a 7-bit Hamming (7, 4)
codeword. It unpacks the inputs as d1‒d4, computes three parity bits with XOR of p1
= d1⊕d2⊕d4, p2 = d1⊕d3⊕d4, and p3 = d2⊕d3⊕d4 and returns them in standard
order: [p1, p2, d1, p3, d2, d3, d4].
Picture .2. introduce_error function
introduce_error function takes a 7-bit codeword and a 1-based position, then
flips that bit with codeword[position-1] ^= 1. This single XOR operation turns 0 → 1
or 1 → 0, injecting exactly one error so the decoder can be tested.
Picture .3. check_parity_groups function
check_parity_group verifies a received 7-bit Hamming word by recomputing
its three parity checks. It XORs the bits of group p3 (position 4 to 7) to get c1, group
p2 (position 2, 3, 6, 7) to get c2, and group p1 (position 1, 3, 5, 7) to get c3. Each ci is
0 if its parity is correct or 1 if it fails. These three results are packed into a “syndrome”
value, formed as the binary number c3, c2, c1 (computed as c3 * 1 + c2 * 2 + c1 * 4).
A syndrome of 0 means no error, any non-zero value (1-7) directly identifies which bit
is wrong. The function returns both the numeric syndrome and the tuple (c3, c2, c1) so
the caller can flip the indicated bit when necessary.
Picture .4. decode_custome
decode_custom recives a 7-bit word, recomputes the parities with
check_parity_group, and prints the resulting syndrome bits. If the syndrome is non-
zero, that value directly indicates which bit is wrong; the line codeword[syndrome - 1]
^ = 1 flips it back to the correct value. Finally, the function extracts and returns the
four original data bits – position 3, 5, 6, 7 of the codeword (codeword [2], [4], [5],
[6]).
Picture .5. Testing code
This test block demonstrates the full Hamming (7,4) workflow. It starts with
the four data bit [1, 0, 1, 1], encodes them into a seven-bit codeword, and then calls
introduce_error to flip one bit (here the index passed is 7, which in Python refers to
the second-to-last element). The corrupted codeword is fed to decode_custom, which
recomputes the parities, corrects the single-bit error, and returns the recovered data.
Finally, the script compares the decoded result with the original data and prints
“Successfully recovered!” if they match, or an error message if they do not.
Result
1. More than 1 bit is error.
Picture .6. More than 1 bit is flipped result
Because a standard Hamming (7,4) code can only correct a single flipped bit,
the second flip confuses the parity logic: the syndrome no longer matches any real
single-bit position, so the decoder either makes a wrong “correction” or leaves the
word unchanged. In either case decoded differs from original_data, and the script
prints “Error could not be corrected”
2. The error bit position to flip is out of range
Picture .7. The bit position to flip is out of range result
In this result, the bit position to flip is out of range (from 1 to 7), the code
return “Invalid position – please enter a value between 1 and 7”.
3. Single-bit error.
Picture .8. single-bit error
The test intentionally flips bit 7 of the 7-bit codeword, changing the last
symbol from 1 to 0. When decode_custom recomputes the parities, all three
checks fail (c1 = c2 = c3 =1), producing a syndrome of 7, which pinpoints the
error at position 7. The decoder flips that bit back to 1, extracts the data (1, 0,
1, 1) and confirms it matches the original input, so it prints “Successfully
recovered!”