The Hamming distance between two binary codes of the same length is the number of positions where their bits differ. 1011 and 1001 differ only in the third bit, so their distance is 1. 0111 and 1000 differ in all four, so their distance is 4.
To compute it, XOR the two codes bit by bit and count the 1s in the result. Each 1 marks a position where they differ.
Why it matters in this course:
- K-maps. Two cells are adjacent exactly when their codes have Hamming distance 1. That is the condition for combining two minterms into one term.
- Gray code. A gray code is a sequence where each code is at distance 1 from the next. That is why K-map axes use it.
- Group size. Inside a group of 2ᵏ cells, the farthest-apart pair is at distance k.
It matters beyond K-maps too. In error detection, a parity bit makes every valid code word at least distance 2 from every other, so any single flipped bit produces an invalid word. Codes with a minimum distance of 3 or more can even locate and correct a single error.
Note that Hamming distance has nothing to do with numeric difference: 7 and 8 are 1 apart as numbers but 4 apart in Hamming distance.