What is Hamming distance?

The number of positions at which two bit strings (or character strings) of the same length differ. It equals the number of 1s in the exclusive OR of the two bit strings. For example, the Hamming distance between 1011010 and 0110011 is 4. The smallest distance between any two codewords of a code is its minimum Hamming distance; a code with minimum Hamming distance d can detect errors of up to d − 1 bits and correct errors of up to (d − 1) ÷ 2 bits, rounded down.

Japanese name: ハミング距離

Hamming distance appears in 3 practice questions across 1 topic, including Information Theory and Coding Basics.

Field: Information Theory and Coding Basics

Error detection and correction capability

The farther apart the codewords, the less likely an error is to turn one into another. In a code with minimum Hamming distance d, an error of d − 1 bits or fewer cannot produce another codeword, so it can be detected, and if the error is at most (d − 1) ÷ 2 bits, the codeword nearest to the received word is the original, so it can be corrected. For example, the repetition code that sends each bit three times has d = 3 and corrects a 1-bit error by majority vote.

Finding it for a linear code

In a linear code, where the exclusive OR of any two codewords is also a codeword, the distance between two codewords equals the number of 1s in the codeword that is their exclusive OR. The minimum Hamming distance can therefore be found as the smallest number of 1s (weight) in a nonzero codeword, without comparing every pair.

Uses in other fields

Hamming distance is used as a general measure that counts the differences between two pieces of data. In evaluating PUFs (physically unclonable functions), the closer the Hamming distance between the responses of different chips is to 50% of all bits, the easier the individual chips are considered to tell apart. In encoding state machines, making the Hamming distance between neighboring states 1 reduces the number of bits that change on a transition.

Try it: questions about Hamming distance

The minimum Hamming distance of a code (the smallest Hamming distance between any two codewords) is 5. Consider using it only for error detection, or only for error correction. Which of the following correctly pairs the number of bit errors that can always be detected with the number that can always be corrected?

  1. ADetection up to 4 bits, correction up to 2 bits
  2. BDetection up to 5 bits, correction up to 2 bits
  3. CDetection up to 4 bits, correction up to 4 bits
Show answer and explanation

Answer: A. Detection up to 4 bits, correction up to 2 bits

In a code with minimum Hamming distance d, an error of up to d − 1 bits never turns a codeword into another codeword, so it can be detected. Correction changes the received bit string to the nearest codeword, so if the number of errors is at most (d − 1) ÷ 2 rounded down, the original codeword remains the nearest. For d = 5, detection works up to 4 bits and correction up to 2 bits.

FAQ

Are Hamming distance and Hamming code the same thing?

No. Hamming distance is a measure that counts the differences between two bit strings, while a Hamming code is a code built to have a minimum Hamming distance of 3, which can correct a 1-bit error. Both are named after Hamming.

Can the Hamming distance between bit strings of different lengths be found?

Hamming distance is defined only between strings of the same length. To count differences including inserted and deleted characters, the edit distance (Levenshtein distance) is used.

Hamming distance in the explanations

Questions that mention Hamming distance (3)

  1. Which of the following is the Hamming distance between the two 7-bit codewords 1011010 and 0110011?
  2. The minimum Hamming distance of a code (the smallest Hamming distance between any two codewords) is 5. Consider using it only for error detection, or only for error correction. Which of the following correctly pairs the number of bit errors that can always be detected with the number that can always be corrected?
  3. Which of the following correctly pairs the minimum Hamming distance of the code consisting of the four codewords 00000, 01011, 10101 and 11110 with the number of bit errors it can always correct?

Books on Information Theory and Coding Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Average code length Binary symmetric channel Forward error correction (FEC) Hamming code Huffman coding Information entropy Kraft inequality Lossless compression Lossy compression Mutual information

Back to the glossary