This will be equivalent to the other answer, but shows how to construct it. For convenience, I will treat the question as "guess a number between 0 and 15", just add 1 to all numbers in questions.
The Hamming code can also be considered to work for a world where a honest person wants to send us a 4-bit number using 7 bits, but the evil messenger (channel) may choose to flip at most one.
First, ask 4 questions which, without lying, would pinpoint which number it is, using the usual binary expansion.
- Is the number 8-15?
- Is the number 4-7 or 12-15?
- Is the number 2-3 or 6-7 or 10-11 or 14-15?
- Is the number odd?
This is equivalent to sending the 4-bit number itself without modification.
Let [Q1] be 1 if they said yes, 0 if they said no to the first question. At this point, without any lie, we know which number it is by reading off the binary number [Q1][Q2][Q3][Q4].
We can do some fancy footwork to double-check for lies:
- Did you answer "yes" to an odd number of: Q1, Q2, and Q4?
- Did you answer "yes" to an odd number of: Q1, Q3, and Q4?
- Did you answer "yes" to an odd number of: Q2, Q3, and Q4?
Note that this is basically asking "did you lie on Q1 or Q2 or Q4", etc.
In other words, also send a specific parity check on (Q1,Q2,Q4), (Q1,Q3,Q4), and (Q2,Q3,Q4).
Now, using these answers, we can exactly pinpoint which of the 7 questions, if any, were lied to:
- If Q1 was the lie, then Q5 and Q6 will be inconsistent (with Q1-4)
- If Q2 was the lie, then Q5 and Q7 will be inconsistent
- If Q3 was the lie, then Q6 and Q7 will be inconsistent
- If Q4 was the lie, then Q5, Q6, and Q7 will be inconsistent
- If Q5-7 was the lie, then only that single question itself will be inconsistent
- If there was no lie, everything is consistent
Exactly one of the 8 cases for consistency of Q5-7 must occur, so we can correct for the lie by flipping the bit corresponding to Q1-4 (if lied to), then reading off the corrected binary number.
This constructs the error syndrome, which is used for decoding, that is, error-correction. In the general formulation of the $(2^{2^n}+2^n-1,2^n)$-Hamming code, the $n$ parity bits are placed at positions $2^k$ (so Q5 is shifted to Q1, Q6 is Q2, Q7 is Q4).