# 汉明距离

两条等长字符串之间的汉明距离，是对应位置上符号不同的位置数目。该概念以数学家和计算机科学家理查德·W·汉明（Richard W. Hamming）命名，他在1950年关于检错纠错码的论文中引入了这一概念，它衡量的是将一条字符串变为另一条所需的最少单字符替换次数<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup>。对于字母表 \(\Sigma\) 上长度为 \(n\) 的字符串 \(x\) 和 \(y\)，

\[ d_H(x, y) = \left| \{ i \mid 1 \le i \le n,\ x_i \ne y_i \} \right|. \]

对于二进制字符串，这就是两条字符串按位异或结果中1的个数<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup>。该度量是信息论、编码理论以及计算机科学与电气工程若干领域的基础<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 两条等长字符串之间不同位置的数目；对于二进制字符串，即按位异或结果的种群计数<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup> |
| 起源 | 由R. W. 汉明在《检错与纠错码》一文中提出，《贝尔系统技术杂志》，第29卷，第147–160页，1950年4月<sup>[2](https://onlinelibrary.wiley.com/doi/10.1002/j.1538-7305.1950.tb00463.x)</sup> |
| 度量性质 | 非负性、不可分辨者同一性、对称性以及三角不等式<sup>[3](https://www.math.ubc.ca/~marcus/Math342/Math342_Lectures3-4.pdf)</sup> |
| 检错 | 最小距离为 \(d\) 的码每个码字最多可检测 \(d - 1\) 个错误<sup>[4](https://bluehawk.monmouth.edu/~rclayton/web-pages/s13-cn/dalfec.html)</sup> |
| 纠错 | 最小距离为 \(d\) 的码每个码字最多可纠正 \(\lfloor (d-1)/2 \rfloor\) 个错误<sup>[4](https://bluehawk.monmouth.edu/~rclayton/web-pages/s13-cn/dalfec.html)</sup> |
| 最大值 | 对于长度为 \(n\) 的二进制字符串，最大距离为 \(n\)，当两条字符串按位互补时达到<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup> |
| 硬件开销 | 可用一次异或和一次种群计数计算；x86的POPCNT指令在一个时钟周期内返回计数<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup> |

## 定义与度量性质

该定义适用于任何字母表，而不仅限于比特：同一字母表上长度相同的两个词 \(x = x_1 \ldots x_n\) 和 \(y = y_1 \ldots y_n\) 的距离为 \(d(x,y) = |\{ 1 \le i \le n : x_i \ne y_i \} |\)<sup>[3](https://www.math.ubc.ca/~marcus/Math342/Math342_Lectures3-4.pdf)</sup>。在定长词空间 \(V = F^n\) 上的函数 \(d : V \times V \to \mathbb{N}\) 称为汉明度量<sup>[5](https://sporadic.stanford.edu/reference/coding/sage/coding/code_bounds.html)</sup>。

汉明距离满足度量的四条公理：\(d(x,y) \ge 0\)；\(d(x,y) = 0\) 当且仅当 \(x = y\)；\(d(x,y) = d(y,x)\)；以及三角不等式 \(d(x,z) \le d(x,y) + d(y,z)\)<sup>[3](https://www.math.ubc.ca/~marcus/Math342/Math342_Lectures3-4.pdf)</sup>。前三条直接由定义可得。三角不等式可通过将距离写成逐位置距离之和来证明，每个逐位置距离为0或1，并在每个位置上应用长度为1的情形<sup>[3](https://www.math.ubc.ca/~marcus/Math342/Math342_Lectures3-4.pdf)</sup>。这些性质赋予定长二进制字符串空间以超立方体的几何解释，通常称为汉明空间，其中距离计算的是所跨越的边数<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup>。

编码理论中始终出现两个相关量。字符串的汉明重量是其与全零字符串的距离，即它所包含的非零符号数目<sup>[6](https://errorcorrectingcodes.wordpress.com/2010/01/12/lecture-material-1-introduction-linear-codes/)</sup>。码 \(C\)（一组码字）的最小距离是任意两个不同码字之间的最小汉明距离；对于线性码，它等于码字的最小非零重量<sup>[5](https://sporadic.stanford.edu/reference/coding/sage/coding/code_bounds.html)</sup>。

## 源于汉明的差错控制工作

汉明在贝尔电话实验室（Bell Telephone Laboratories）从事大型计算机器工作时发展了这一概念，这些机器需要执行大量操作而最终结果中不能出现任何错误。他将电话中央局与数字计算机进行了对比：前者通过独立的并行路径将错误控制在可控范围内，而后者中单个未被检测到的故障会使机器所有后续操作失效<sup>[7](https://zoo.cs.yale.edu/classes/cs323/doc/Hamming.pdf)</sup>。

1946–47年间，汉明使用的是Model V继电器计算机，该机按照乔治·斯蒂比茨（George Stibitz）开创的思路建造，用五中取二码表示每个十进制数字：恰好两个继电器吸合、三个继电器释放。任何单个继电器故障，无论是未能闭合的继电器还是卡在断开状态的继电器，都能被立即检测到，机器在放弃一个问题之前会进行三次尝试<sup>[8](https://www.comap.org/membership/member-resources/item/hamming-distance-and-its-discovery)</sup>。一个广为流传的说法是，1947年继电器计算机的一次周末运行很早就失败了，导致汉明周一没有任何结果，于是他问为什么一台能够检测错误的机器不能同时定位并修复错误；他的解决方案增加了冗余奇偶校验位，使得单比特错误可以被定位和纠正，由此产生的方案被称为汉明码<sup>[9](https://history.computer.org/pioneers/hamming.html)</sup>。

他1950年的论文《检错与纠错码》发表在《贝尔系统技术杂志》第29卷第2期第147–160页<sup>[2](https://onlinelibrary.wiley.com/doi/10.1002/j.1538-7305.1950.tb00463.x)</sup>。该文给出了单错误检测、单错误纠正以及单错误纠正加双错误检测的显式解决方案，并奠定了一个此后发展出大量文献的领域的基础<sup>[10](https://garfield.library.upenn.edu/classics1982/A1982NY35700001.pdf)</sup>。

## 检错与纠错

距离的实际重要性在于其与码的差错控制能力之间的关系。汉明距离为 \(d\) 的两个码字需要 \(d\) 个单比特错误才能将一个变为另一个，因此最小距离为 \(d\) 的码可以检测码字中最多 \(d - 1\) 个错误，并纠正最多 \(\lfloor (d-1)/2 \rfloor\) 个错误<sup>[4](https://bluehawk.monmouth.edu/~rclayton/web-pages/s13-cn/dalfec.html)</sup>。等价地，检测 \(d\) 个单比特错误需要最小距离 \(d + 1\)，纠正 \(d\) 个单比特错误需要最小距离 \(2d + 1\)<sup>[11](https://www.eecs.umich.edu/courses/eecs373.w05/lecture/errorcode.html)</sup>。

其机制是几何的。单比特错误将接收到的词移动到距发送码字距离为1的位置；如果所有码字之间至少相隔3，则任何含有一个错误的词都不会被误认为另一个码字，而最近邻解码——将任何接收到的词映射到汉明距离最小的有效码字——可以恢复原始码字<sup>[12](https://web.mit.edu/6.02/www/s2012/handouts/5.pdf)</sup>。汉明最初的[7,4]码的最小距离为3，可以纠正7比特码字中的任何单比特错误<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup>。检测和纠正可以权衡：一个需要纠正 \(n_C\) 个错误并检测另外 \(n_D\) 个错误的码需要相应更大的最小距离<sup>[13](https://www.sciencedirect.com/topics/engineering/hamming-distance)</sup>。

## 计算距离

对于二进制字符串，计算过程是一次按位异或，然后对结果中的1进行种群计数，两者都得到现代处理器的原生支持；x86的POPCNT指令和GCC的 `__builtin_popcount` 内建函数在一个时钟周期内返回计数，使得该操作在实践中开销可忽略不计<sup>[1](https://technav.ieee.org/topic/hamming-distance/)</sup>。对任何字母表的直接实现是一次遍历，统计不匹配的位置<sup>[14](https://www.levenshtein.net/edit-distance-variants)</sup>。

汉明空间中的大规模最近邻搜索是相似性搜索库的标准能力。Faiss通过 `IndexBinaryFlat`、`IndexBinaryIVF`、`IndexBinaryHNSW` 和 `IndexBinaryHash`/`IndexBinaryMultiHash` 对二进制向量建立汉明距离索引，将维度为 \(d\) 的向量存储为 \(d/8\) 字节，并利用种群计数CPU指令优化穷举距离计算<sup>[15](https://github.com/facebookresearch/faiss/wiki/Binary-Indexes)</sup>。在计算机视觉中，OpenCV的暴力匹配器对ORB、BRIEF和BRISK等二进制描述子使用 `NORM_HAMMING` 距离<sup>[16](https://docs.opencv.org/4.5.2/dc/dc3/tutorial_py_matcher.html)</sup>。用于近似重复图像检测的感知哈希流水线同样通过汉明距离比较定长哈希<sup>[17](https://arxiv.org/abs/2212.08035)</sup>。

## 与编辑距离的关系

汉明距离只允许替换，并要求序列等长，而莱文斯坦编辑距离还允许插入和删除<sup>[14](https://www.levenshtein.net/edit-distance-variants)</sup>。对于等长字符串，莱文斯坦距离永远不会更大，但当短的插入或删除使长后缀的对齐发生偏移时，差距可能很大：对于 \(x = \text{abc}\) 和 \(y = \text{bca}\)，汉明距离为3，因为所有三个对齐位置都不同，而莱文斯坦距离为2，使用一次删除和一次插入<sup>[14](https://www.levenshtein.net/edit-distance-variants)</sup>。针对不等长的变通方法，例如用虚拟符号填充较短的词，可能使非常相似但长度不同的词看起来相距很远，这是变长文本通常更倾向于使用莱文斯坦距离的原因之一<sup>[18](https://citeseerx.ist.psu.edu/document?doi=7365feaddedbbd7fd369a09cfb7f664d4a642696&repid=rep1&type=pdf)</sup>。

## 在生物学和理论中的应用

在生物信息学中，归一化汉明距离——两条对齐序列中对应符号不同的位置所占的比例——是序列差异的最简单度量。它低估了真实的进化距离，因为它忽略了回复突变和同一位点的多次突变，因此在其之上应用基于模型的校正；在Jukes–Cantor模型下（该模型假设独立替换且碱基频率相等），校正后的估计为

\[ JC(A,B) = -\frac{3}{4} \ln\left( 1 - \frac{4}{3} H(A,B) \right). \]

<sup>[19](https://academic.oup.com/bib/article/22/3/bbaa147/5879060)</sup>

在DNA测序纠错中，大多数工具使用汉明距离而非莱文斯坦距离，因为在主流的Illumina平台上替换错误的发生频率比插入和删除错误高一个数量级，并且仅替换方法在计算上更便宜；Reptile、Hammer和BayesHammer等工具构建汉明图，其中k-mer在距离低于低阈值时相连<sup>[20](https://pmc.ncbi.nlm.nih.gov/articles/PMC4719071/)</sup>。

该距离还支撑着理论结果。在间隙-汉明通信问题中，两方持有长度为 \(n\) 的向量，保证它们相差至少 \(n/2 + \sqrt{n}\) 或至多 \(n/2 - \sqrt{n}\) 个位置，必须判定哪种情况成立；该问题的随机单向通信复杂度具有线性下界<sup>[21](https://cims.nyu.edu/~regev/toc/articles/v004a006/index.html)</sup>。研究继续扩展该度量本身，包括扩展域上码的折叠汉明距离以及推广经典汉明度量的基于偏序集的度量<sup>[22](https://link.springer.com/article/10.1007/s10623-025-01718-5)</sup><sup> • </sup><sup>[23](https://www.sciencedirect.com/science/article/pii/S1071579725000887)</sup>。

## References

1. [Hamming Distance, IEEE Technology Navigator](https://technav.ieee.org/topic/hamming-distance/)
2. [Hamming, R. W. (1950). Error Detecting and Error Correcting Codes. Bell System Technical Journal, 29: 147–160.](https://onlinelibrary.wiley.com/doi/10.1002/j.1538-7305.1950.tb00463.x)
3. [Math 342 Lecture Notes, University of British Columbia](https://www.math.ubc.ca/~marcus/Math342/Math342_Lectures3-4.pdf)
4. [Data Link Flow & Error Control, CS 414 & 514 Lecture Notes, Monmouth University](https://bluehawk.monmouth.edu/~rclayton/web-pages/s13-cn/dalfec.html)
5. [Bounds for parameters of codes, Sage Reference Manual: Coding Theory](https://sporadic.stanford.edu/reference/coding/sage/coding/code_bounds.html)
6. [Lecture material 1: Introduction, linear codes, CMU Introduction to Coding Theory](https://errorcorrectingcodes.wordpress.com/2010/01/12/lecture-material-1-introduction-linear-codes/)
7. [Hamming, R. W. Error Detecting and Error Correcting Codes (full text PDF)](https://zoo.cs.yale.edu/classes/cs323/doc/Hamming.pdf)
8. [Hamming Distance and Its Discovery, COMAP](https://www.comap.org/membership/member-resources/item/hamming-distance-and-its-discovery)
9. [Computer Pioneers: Richard Wesley Hamming, IEEE Computer Society](https://history.computer.org/pioneers/hamming.html)
10. [Citation Classic: Hamming R W, Error detecting and error correcting codes, Garfield](https://garfield.library.upenn.edu/classics1982/A1982NY35700001.pdf)
11. [Error detecting and correcting codes, EECS 373, University of Michigan](https://www.eecs.umich.edu/courses/eecs373.w05/lecture/errorcode.html)
12. [MIT 6.02 Lecture Notes, Handout 5](https://web.mit.edu/6.02/www/s2012/handouts/5.pdf)
13. [Hamming Distance, ScienceDirect Topics](https://www.sciencedirect.com/topics/engineering/hamming-distance)
14. [Edit Distance Variants: Models, Metrics and Generalizations, Levenshtein.net](https://www.levenshtein.net/edit-distance-variants)
15. [Binary Indexes, Faiss Wiki, Facebook Research](https://github.com/facebookresearch/faiss/wiki/Binary-Indexes)
16. [OpenCV: Feature Matching](https://docs.opencv.org/4.5.2/dc/dc3/tutorial_py_matcher.html)
17. [Hamming Distributions of Popular Perceptual Hashing Techniques, arXiv](https://arxiv.org/abs/2212.08035)
18. [Computing Error-Detecting Capabilities of Formal Languages, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=7365feaddedbbd7fd369a09cfb7f664d4a642696&repid=rep1&type=pdf)
19. [Distance-based phylogenetic inference from typing data, Briefings in Bioinformatics](https://academic.oup.com/bib/article/22/3/bbaa147/5879060)
20. [Denoising DNA deep sequencing data, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC4719071/)
21. [The One-Way Communication Complexity of Hamming Distance, Theory of Computing](https://cims.nyu.edu/~regev/toc/articles/v004a006/index.html)
22. [Linear codes in the folded Hamming distance and the quasi MDS property, Designs, Codes and Cryptography](https://link.springer.com/article/10.1007/s10623-025-01718-5)
23. [Shortest-path and antichain metrics, Finite Fields and Their Applications](https://www.sciencedirect.com/science/article/pii/S1071579725000887)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Discrete mathematics

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/hamming-distance

English original: https://www.edgepedia.org/hamming-distance
