SMALLTALK LAB
Hamming(7,4): locate and repair one flipped bit
Turn four data bits into a seven-bit codeword, use XOR parity to locate one error, and prove the limit by making two errors fool the decoder. Finish with all 112 single-bit corruption cases.
Before you begin
Open a Workspace in native Squeak 6.1. Select one complete checkpoint and choose Print it. Each checkpoint is independent and installs no classes. Arrays list positions 1 through 7, not a printed binary integer; data bits are least-significant first. Inputs are integers 0..15 and arrays of exactly seven bits (0 or 1). Use Do it for the separate Morphic board, changing stage from 0 through 4.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
01 - Assign seven positions to four data bits
Use even parity and one-based positions. Positions 1, 2 and 4 hold parity; 3, 5, 6 and 7 hold the four data bits, least-significant bit first. A parity group contains every position whose binary address includes its mask. For example, position 7 (binary 111) belongs to all three groups. The arrays below list the groups for masks 1, 2 and 4.
| positions |
positions := (1 to: 7) asArray.
#(1 2 4) collect: [:mask |
positions select: [:position |
(position bitAnd: mask) ~= 0]]Expected: #(#(1 3 5 7) #(2 3 6 7) #(4 5 6 7))

Try it: Write the seven addresses as 001 through 111. Read their rightmost, middle and leftmost bits to recover these groups. Position zero is deliberately absent.
02 - Encode a four-bit message with even parity
The integer 11 has low-to-high data bits #(1 1 0 1). Each parity bit is the XOR of the data bits in its group. Adding that parity bit makes the full group XOR zero, meaning an even number of ones. Each group contains its own parity position and no other parity position, so the three assignments do not interfere.
| encode |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
encode value: 11Expected: #(1 0 1 0 1 0 1)

Try it: Count the ones in positions #(1 3 5 7): there are four. The other two groups each contain two. This encoder assumes an integer in 0..15; it does not validate input.
03 - Read all three parity failures as a syndrome
The syndrome is a three-bit number: failed group 1 contributes 1, group 2 contributes 2, and group 4 contributes 4. XORing the addresses of every one bit computes the same number directly. In each binary column, XOR records whether an odd number of included bits is set. An encoded, unchanged word therefore has syndrome zero.
| encode syndrome codeword checks |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
syndrome := [:word | | result |
result := 0.
1 to: 7 do: [:position |
(word at: position) = 1 ifTrue: [
result := result bitXor: position]].
result].
codeword := encode value: 11.
checks := #(1 2 4) collect: [:mask |
(1 to: 7) inject: 0 into: [:parity :position |
(position bitAnd: mask) = 0
ifTrue: [parity]
ifFalse: [parity bitXor: (codeword at: position)]]].
{checks. syndrome value: codeword}Expected: #(#(0 0 0) 0)

Try it: A zero syndrome means the received word satisfies the checks. Without an error bound it does not prove that the original message arrived unchanged.
04 - Locate a flipped bit from its binary address
Flip position 6 by XORing its bit with 1. That position belongs to groups 2 and 4, but not group 1, so the syndrome is 2 + 4 = 6. Starting with a valid codeword, toggling bit i toggles exactly the parity columns in the binary address of i. This is why the syndrome identifies a single corrupted position.
| encode syndrome sent received |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
syndrome := [:word | | result |
result := 0.
1 to: 7 do: [:position |
(word at: position) = 1 ifTrue: [
result := result bitXor: position]].
result].
sent := encode value: 11.
received := sent copy.
received at: 6 put: ((received at: 6) bitXor: 1).
{received. syndrome value: received.
sent = received}Expected: #(#(1 0 1 0 1 1 1) 6 false)

Try it: Use position 4 instead. A parity bit can be corrupted too: the syndrome becomes 4, and the data bits themselves remain unchanged.
05 - Correct one error without modifying the received array
Under the explicit assumption of at most one flipped bit per seven-bit word, a nonzero syndrome tells us which bit to toggle. Copy the received array before repairing it. Syndrome zero needs no toggle; index zero is not a Squeak Array position. The second check confirms parity after correction, while comparison with the known sent fixture confirms actual recovery.
| encode syndrome sent received repaired location |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
syndrome := [:word | | result |
result := 0.
1 to: 7 do: [:position |
(word at: position) = 1 ifTrue: [
result := result bitXor: position]].
result].
sent := encode value: 11.
received := sent copy.
received at: 6 put: ((received at: 6) bitXor: 1).
repaired := received copy.
location := syndrome value: repaired.
location ~= 0 ifTrue: [
repaired at: location
put: ((repaired at: location) bitXor: 1)].
{location. repaired = sent. syndrome value: repaired.
received = sent}Expected: #(6 true 0 false)

Try it: Remove the corruption line. The result becomes #(0 true 0 true). Keeping a copy separates received evidence from the proposed repair.
06 - Extract data only after the correction decision
Decoding reads positions 3, 5, 6 and 7 with weights 1, 2, 4 and 8. For this example a flip in position 6 changes the uncorrected data from 11 to 15. Repair first, then extract. Real receivers do not have the original word for comparison: they rely on the channel error model, not on a zero syndrome as proof of authenticity.
| encode syndrome decode received repaired location |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
syndrome := [:word | | result |
result := 0.
1 to: 7 do: [:position |
(word at: position) = 1 ifTrue: [
result := result bitXor: position]].
result].
decode := [:word | | number |
number := 0.
#(3 5 6 7) withIndexDo: [:position :bit |
number := number +
((word at: position) bitShift: bit - 1)].
number].
received := encode value: 11.
received at: 6 put: ((received at: 6) bitXor: 1).
repaired := received copy.
location := syndrome value: repaired.
location ~= 0 ifTrue: [
repaired at: location
put: ((repaired at: location) bitXor: 1)].
{decode value: received. decode value: repaired}Expected: #(15 11)

Try it: Change the flip to position 1. Both extracted numbers become 11, but repairing that parity bit still restores the full codeword.
07 - Watch two errors fool a single-error decoder
Two flips at positions 3 and 5 produce syndrome 3 XOR 5 = 6. A decoder that assumes one error flips position 6 as well, landing on a different valid codeword. The checks now pass but the decoded message is 12, not 11. These three parity bits do not distinguish a single error from two errors: this is Hamming(7,4), not an extended SECDED code.
| encode syndrome decode sent damaged repaired location |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
syndrome := [:word | | result |
result := 0.
1 to: 7 do: [:position |
(word at: position) = 1 ifTrue: [
result := result bitXor: position]].
result].
decode := [:word | | number |
number := 0.
#(3 5 6 7) withIndexDo: [:position :bit |
number := number +
((word at: position) bitShift: bit - 1)].
number].
sent := encode value: 11.
damaged := sent copy.
#(3 5) do: [:position |
damaged at: position
put: ((damaged at: position) bitXor: 1)].
location := syndrome value: damaged.
repaired := damaged copy.
repaired at: location
put: ((repaired at: location) bitXor: 1).
{location. syndrome value: repaired.
decode value: repaired. repaired = sent}Expected: #(6 0 12 false)

Try it: The raw nonzero syndrome flags a parity violation for these two errors. It does not say there were two errors. Automatic single-error correction can silently turn that violation into wrong data.
08 - Exhaust all 16 messages and 112 single-bit errors
The final independent experiment checks all four-bit messages unchanged, then all seven single-bit flips for each message. It requires the syndrome to name the injected error, the repaired word to equal the encoder output, the repaired syndrome to be zero and the extracted message to match. The return value reports 16 clean cases, 112 corrupted cases and their combined verdict. It tests the promised error bound, not arbitrary corruption.
| encode syndrome decode clean tested correct |
encode := [:number | | word |
word := Array new: 7 withAll: 0.
#(3 5 6 7) withIndexDo: [:position :bit |
word at: position put: (number bitAt: bit)].
#(1 2 4) do: [:mask | | parity |
parity := 0.
#(3 5 6 7) do: [:position |
(position bitAnd: mask) ~= 0 ifTrue: [
parity := parity bitXor: (word at: position)]].
word at: mask put: parity].
word].
syndrome := [:word | | result |
result := 0.
1 to: 7 do: [:position |
(word at: position) = 1 ifTrue: [
result := result bitXor: position]].
result].
decode := [:word | | number |
number := 0.
#(3 5 6 7) withIndexDo: [:position :bit |
number := number +
((word at: position) bitShift: bit - 1)].
number].
clean := 0. tested := 0. correct := true.
0 to: 15 do: [:message | | sent |
sent := encode value: message.
((syndrome value: sent) = 0 and: [
(decode value: sent) = message])
ifTrue: [clean := clean + 1].
1 to: 7 do: [:error | | received location repaired |
received := sent copy.
received at: error
put: ((received at: error) bitXor: 1).
location := syndrome value: received.
repaired := received copy.
location ~= 0 ifTrue: [
repaired at: location
put: ((repaired at: location) bitXor: 1)].
tested := tested + 1.
correct := correct and: [location = error and: [
repaired = sent and: [
(syndrome value: repaired) = 0 and: [
(decode value: repaired) = message]]]]]].
{clean. tested. correct and: [clean = 16]}Expected: #(16 112 true)

Try it: Keep the exact count as well as the Boolean: a loop that never runs can otherwise pass a universal test. Add a separate two-error suite to demonstrate miscorrection, not to claim correction beyond the contract.
Watch the computed states

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.
Reason about correctness and limits
The code uses even parity, one-based positions and low-to-high data bits. A different bit order can be valid, but encoder, decoder and tests must agree. Array display order here is not the usual most-significant-first binary notation.
Proof: a valid word has syndrome zero. XOR is associative and toggling position i changes the syndrome by i. The seven addresses are distinct and nonzero, so every single-bit flip yields its own unique location. Toggling that location again restores the original word, including parity-bit errors.
The minimum distance is exactly three. A nonzero one-bit difference has nonzero syndrome i. A two-bit difference has syndrome i XOR j, nonzero for distinct i and j. A three-bit difference at positions 1, 2 and 3 has zero syndrome, establishing distance three. This supports correcting one error, or detecting up to two without attempting correction; it does not support distinguishing double errors while correcting single errors.
Two different flips always give a nonzero raw syndrome, but it is also the address of another bit. The automatic repair shown here therefore miscorrects every exactly-two-bit corruption. Three or more errors can have zero syndrome. Extended Hamming SECDED adds an overall parity bit and different decision logic; simply calling this code SECDED would be wrong.
This is redundancy, not compression: four payload bits become seven transmitted bits, a rate of 4/7 and 75% overhead relative to payload. For fixed Hamming(7,4), encoding and decoding take bounded constant work per block; for B blocks the work is O(B). The teaching implementation allocates seven-element Arrays and copies, much larger than seven packed physical bits.
Luhn validates decimal check digits for common transcription mistakes; it does not locate and repair arbitrary bit flips. Neither Luhn nor Hamming parity is encryption or authentication. Hamming correction needs a credible at-most-one-error-per-word assumption, and is unsuitable as protection against malicious changes.
The blocks deliberately omit input validation and framing. Validate ranges and bit values at a boundary, specify how four-bit chunks map to bytes, and handle incomplete final chunks in any real protocol. The exhaustive suite verifies this implementation over its finite input contract, not the behavior of a physical channel.
Three practice challenges
Repair a parity-bit error by hand
Encode message 6 using the low-to-high convention. Flip position 2, predict the received array and syndrome, then repair and decode it. Explain why unchanged data bits do not mean the whole word is intact.
Show solution
Message 6 has data #(0 1 1 0). Its codeword is #(1 1 0 0 1 1 0). Flipping position 2 gives #(1 0 0 0 1 1 0), with syndrome 2. Toggling position 2 restores the codeword and decoding gives 6. The payload was unchanged even before repair, but the parity violation still records corruption and must not be ignored.
Count the misleading two-error cases
Extend the final test with every pair of distinct positions i < j for every message. Before correction, count nonzero syndromes. After applying the single-error rule, count words that satisfy parity but differ from sent. Predict both counts and explain them without relying only on execution.
Show solution
There are 7 * 6 / 2 = 21 unordered pairs per message, so each count is 16 * 21 = 336. The raw syndrome is i XOR j, which is nonzero and different from both i and j. Flipping that third position leaves three differences with XOR zero. All 336 proposed repairs pass parity and are wrong codewords. To generate pairs, use 1 to: 6 do: [:i | i + 1 to: 7 do: [:j | ...]].
Explain the perfect packing and its limitation
How many seven-bit words exist, and how many lie within one flip of each valid codeword? Use the distance-three argument to explain whether any received pattern remains available as an unambiguous double-error warning. Then propose what an extra parity bit would need to change.
Show solution
There are 2 raisedTo: 7 = 128 words and 16 valid codewords. Each has one unchanged word plus seven one-flip neighbors, so 16 * 8 = 128. Distance three makes those radius-one neighborhoods disjoint: a word within one flip of two codewords would put those codewords at distance at most two. Every received pattern is therefore assigned to some single-error neighborhood, leaving no reserved double-error warning pattern. An eighth overall even-parity bit adds a check: with a nonzero seven-bit syndrome, odd total parity indicates a single error under the up-to-two-error assumption, while even total parity indicates two errors and must block correction. A zero syndrome with odd total parity identifies an error in the extra parity bit. This is a different, extended code and still does not guarantee safety for arbitrary numbers of errors.
Continue learning
Integer Bitsets | Luhn Checksum | Collections | Testing
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.