{
  "id": "hamming-distance",
  "title": "Hamming Distance",
  "lang": "en",
  "source_lang": "en",
  "summary": "Hamming distance counts the positions at which two equal-length sequences differ, providing a fundamental measure for error-correcting codes and comparisons of discrete data.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Hamming metric"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/hamming-distance/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/hamming-distance/",
      "json": "https://aiwiki.page/zh/hamming-distance.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Hamming distance is the number of positions at which two sequences of equal length contain different symbols. It measures the substitutions required to transform one sequence into the other without inserting, deleting, or rearranging symbols. Named after Richard Hamming, it is fundamental to error-correcting codes and is widely used to compare binary strings, categorical records, and encoded data."
    },
    {
      "heading": "Definition and examples",
      "text": "Let \\(x=(x_1,\\ldots,x_n)\\) and \\(y=(y_1,\\ldots,y_n)\\) be sequences over the same alphabet \\(\\Sigma\\). Their Hamming distance is\n\n\\[d_H(x,y)=\\sum_{i=1}^{n}\\mathbf{1}[x_i\\ne y_i],\\]\n\nwhere the indicator \\(\\mathbf{1}[x_i\\ne y_i]\\) equals \\(1\\) when the symbols differ and \\(0\\) otherwise.\n\nFor example,\n\n\\[x=1011101,\\qquad y=1001001\\]\n\ndiffer at the third and fifth positions, so \\(d_H(x,y)=2\\). The definition is not restricted to binary data: the strings karolin and kathrin have Hamming distance \\(3\\).\n\nThe alphabet may consist of bits, letters, nucleotide symbols, or categories. The identity of a symbol matters, but its numerical magnitude does not: replacing \\(1\\) with \\(2\\) contributes the same distance as replacing \\(1\\) with \\(100\\).\n\nFor sequences of positive length, the normalized Hamming distance is\n\n\\[\\delta_H(x,y)=\\frac{d_H(x,y)}{n}.\\]\n\nIt lies between \\(0\\) and \\(1\\) and represents the fraction of mismatching positions. Some software libraries use “Hamming distance” to mean this normalized quantity, so the convention must be checked when interpreting results."
    },
    {
      "heading": "Mathematical properties",
      "text": "For a fixed length \\(n\\), Hamming distance is a metric on \\(\\Sigma^n\\). It satisfies:\n\n- Non-negativity: \\(d_H(x,y)\\ge 0\\).\n- Identity of indiscernibles: \\(d_H(x,y)=0\\) exactly when \\(x=y\\).\n- Symmetry: \\(d_H(x,y)=d_H(y,x)\\).\n- Triangle inequality: \\(d_H(x,z)\\le d_H(x,y)+d_H(y,z)\\).\n\nThe triangle inequality follows position by position: if \\(x_i\\ne z_i\\), then at least one of \\(x_i\\ne y_i\\) or \\(y_i\\ne z_i\\) must hold.\n\nThe resulting space is called a Hamming space. For binary sequences, it can be represented as an \\(n\\)-dimensional hypercube: vertices are binary strings, and an edge connects strings differing in exactly one position. Hamming distance is then the length of a shortest path along the edges.\n\nHamming weight\n\nThe Hamming weight \\(w_H(x)\\) of a sequence over an alphabet with a designated zero symbol is the number of its nonzero coordinates. For binary strings, it is simply the number of \\(1\\) bits.\n\nBinary Hamming distance can therefore be expressed as\n\n\\[d_H(x,y)=w_H(x\\oplus y),\\]\n\nwhere \\(\\oplus\\) denotes bitwise exclusive OR. For vectors over a finite field, the analogous identity is\n\n\\[d_H(x,y)=w_H(x-y).\\]\n\nFor binary vectors regarded as real-valued vectors, Hamming distance also equals their Manhattan distance and their squared Euclidean distance:\n\n\\[d_H(x,y) =\\sum_{i=1}^{n}|x_i-y_i| =\\sum_{i=1}^{n}(x_i-y_i)^2.\\]\n\nThese equalities depend on the coordinates being binary; they do not generally hold for arbitrary categorical or numerical data."
    },
    {
      "heading": "Historical development",
      "text": "Hamming distance takes its name from Richard W. Hamming, whose 1950 paper Error Detecting and Error Correcting Codes established foundational results on detecting and correcting errors in digital information. His work at Bell Laboratories connected the construction of codes with the separation between their valid codewords.\n\nHamming codes became an important family of single-error-correcting codes. Their design illustrates the central principle of distance-based coding: valid messages are represented by strings sufficiently separated that a limited number of symbol errors can be detected or corrected.\n\nThe distance subsequently became a standard tool throughout coding theory and in applications involving fixed-position comparisons of discrete data."
    },
    {
      "heading": "Role in error detection and correction",
      "text": "A block code is a set \\(C\\subseteq\\Sigma^n\\) of valid codewords. For a code with at least two codewords, its minimum distance is\n\n\\[d_{\\min} =\\min_{\\substack{x,y\\in C\\\\x\\ne y}}d_H(x,y).\\]\n\nThis quantity determines its guaranteed protection against symbol substitutions.\n\nError detection\n\nA code can detect every pattern of at most \\(s\\) substitutions if\n\n\\[d_{\\min}\\ge s+1.\\]\n\nFewer than \\(d_{\\min}\\) substitutions cannot turn one valid codeword into another. Thus, a received word containing such errors must lie outside the code and can be recognized as invalid.\n\nError correction\n\nA code can uniquely correct every pattern of at most \\(t\\) substitutions if\n\n\\[d_{\\min}\\ge 2t+1.\\]\n\nEquivalently, its guaranteed correction radius is\n\n\\[t=\\left\\lfloor\\frac{d_{\\min}-1}{2}\\right\\rfloor.\\]\n\nThe reason is geometric: the sets of words within distance \\(t\\) of distinct codewords must not overlap. Otherwise, the same received word could be consistent with two transmitted codewords.\n\nFor example, the binary repetition code\n\n\\[C=\\{000,111\\}\\]\n\nhas minimum distance \\(3\\), so it corrects any single-bit error by majority decoding.\n\nIf error locations are known, the affected symbols are called erasures. A code of minimum distance \\(d_{\\min}\\) can uniquely recover from up to \\(d_{\\min}-1\\) erasures. More generally, it guarantees unique recovery from \\(t\\) substitutions and \\(e\\) erasures when\n\n\\[2t+e<d_{\\min}.\\]\n\nThese guarantees concern substitution errors and known erasures, not insertions or deletions.\n\nHamming balls and the packing bound\n\nA Hamming ball of radius \\(t\\) around \\(x\\) contains all words whose distance from \\(x\\) is at most \\(t\\). Over an alphabet of size \\(q\\), its size is\n\n\\[V_q(n,t)=\\sum_{i=0}^{t}\\binom{n}{i}(q-1)^i, \\qquad 0\\le t\\le n.\\]\n\nThere are \\(\\binom{n}{i}\\) ways to choose the differing positions and \\((q-1)^i\\) ways to replace their symbols.\n\nIf a code with \\(M\\) codewords corrects every pattern of at most \\(t\\) substitutions, its correction balls are disjoint. Consequently,\n\n\\[M V_q(n,t)\\le q^n.\\]\n\nThis is the Hamming bound, also called the sphere-packing bound. It limits how many codewords can fit into a fixed-length space while maintaining a specified correction capability."
    },
    {
      "heading": "Computation",
      "text": "For general sequences, a direct algorithm compares corresponding symbols and counts mismatches. Assuming constant-time symbol comparisons, its time complexity is \\(O(n)\\), with \\(O(1)\\) auxiliary space.\n\nFor packed binary data, computation typically uses two operations:\n\n- Apply bitwise exclusive OR to the inputs.\n- Count the \\(1\\) bits in the result.\n\nThe second operation is called a population count. Many processors provide instructions for it. If an \\(n\\)-bit sequence is stored in machine words of width \\(w\\), the comparison requires \\(O(\\lceil n/w\\rceil)\\) word operations when exclusive OR and population count each take constant time per word. Unused padding bits must be excluded.\n\nLarge-scale comparisons can exploit vector instructions or parallel computing. Nevertheless, efficiently comparing one pair does not automatically make searching a large collection efficient: finding close matches also depends on the representation and search method.\n\nThe unit of comparison matters. A mismatch between two bytes counts as one symbol mismatch in a byte-level calculation, but may represent several differing bits. Likewise, comparing Unicode text by encoded bytes, code points, or user-perceived characters can produce different results."
    },
    {
      "heading": "Applications",
      "text": "Communications and storage\n\nHamming distance measures separation between valid encoded messages in digital transmission and storage. It supports the analysis of codes designed to resist corruption, including binary block codes and symbol-based systems such as Reed–Solomon codes. For the latter, distance normally counts differing field symbols rather than differing individual bits.\n\nBinary representations and similarity search\n\nIn information retrieval and computer vision, objects can be represented by compact binary descriptors. Hamming distance provides a fast way to compare them, making it useful in matching image features and searching collections of binary fingerprints.\n\nThe usefulness of the comparison depends on the encoding. A small distance indicates similarity only when the representation has been designed to preserve relevant similarities.\n\nIn particular, ordinary cryptographic hashes are not similarity-preserving encodings: similar inputs are not expected to produce nearby hash values.\n\nMachine learning\n\nIn machine learning, Hamming distance can compare fixed-length categorical feature vectors. It is also used in multilabel classification, where each label is represented by a binary indicator. The normalized distance between the predicted and true label vectors gives the fraction of incorrectly predicted label indicators, commonly called Hamming loss.\n\nThis differs from exact-match evaluation, which treats an entire prediction as incorrect whenever any label differs.\n\nBiological sequences\n\nFor equal-length, aligned DNA or protein sequences, Hamming distance counts mismatching positions. It is a simple measure of observed sequence difference, but it does not model insertion and deletion events, repeated substitutions at the same site, or differing probabilities of biological substitutions."
    },
    {
      "heading": "Related measures and limitations",
      "text": "Edit distance\n\nHamming distance assumes fixed positional correspondence and, in its standard form, equal-length inputs. Edit distance can instead allow insertions, deletions, and substitutions.\n\nFor example, abcd and bcda have Hamming distance \\(4\\), but their unit-cost Levenshtein distance is \\(2\\): delete the initial a and insert it at the end. A positional shift can therefore create many Hamming mismatches even when the strings are closely related under an editing model.\n\nUnequal importance of positions\n\nOrdinary Hamming distance assigns the same cost to every mismatch. A weighted version is\n\n\\[d_w(x,y)=\\sum_{i=1}^{n}w_i\\mathbf{1}[x_i\\ne y_i].\\]\n\nFor fixed strictly positive weights, this remains a metric. With nonnegative weights that include zeros, distinct sequences can have zero distance, producing a pseudometric.\n\nNeither version automatically captures differences in similarity among symbols: every unequal pair at a given position receives the same penalty.\n\nRepresentation dependence\n\nHamming distance measures differences in an encoding, not an intrinsic similarity between the represented objects. Under one-hot encoding, changing one categorical value to another changes two bits, although it changes only one original categorical position.\n\nLikewise, a small numerical change can flip many bits in a binary integer representation. Hamming distance is therefore not generally an appropriate measure of numerical closeness.\n\nNormalization also does not remove all dependence on the alphabet. If two symbols are independently and uniformly drawn from an alphabet of size \\(q\\), their probability of differing is \\(1-1/q\\). Expected normalized distances between random strings therefore vary with alphabet size."
    }
  ],
  "links": [
    "error-correcting-code",
    "metric-space",
    "bell-labs",
    "algorithm",
    "time-complexity",
    "parallel-computing",
    "bit",
    "unicode",
    "reed-solomon-code",
    "information-retrieval",
    "computer-vision",
    "cryptographic-hash-function",
    "machine-learning",
    "multilabel-classification",
    "dna",
    "protein",
    "edit-distance",
    "one-hot-encoding"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}