# 格雷码

格雷码是对 \( 2^{n} \) 个 n 位二进制数的一种排序，其中相邻两项恰有一个比特位不同。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup> 最著名的实例是二进制反射格雷码（BRGC），得名于贝尔电话实验室的研究员弗兰克·格雷（Frank Gray），他为其在脉冲编码通信中的用途申请了美国专利 2,632,058，该专利于 1947 年 11 月 13 日提交，1953 年 3 月 17 日授权。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup><sup> • </sup><sup>[2](https://patents.google.com/patent/US2632058A/en)</sup> 单比特变化特性使这种编码在读取可能正在变化的值时极具价值：旋转编码器、模数转换器，以及跨越异步时钟域的计数器。<sup>[3](https://www.ia.omron.com/support/faq/answer/34/faq00941/)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 对 \( 2^{n} \) 个二进制数的一种排序，使得相邻两项之间只有一个比特发生变化；4 位及以上的编码并不唯一，即使允许比特的置换或反转也是如此<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup> |
| 命名 | 以贝尔电话实验室的弗兰克·格雷命名；美国专利 2,632,058“脉冲编码通信”，1947 年 11 月 13 日提交，1953 年 3 月 17 日授权<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup><sup> • </sup><sup>[2](https://patents.google.com/patent/US2632058A/en)</sup> |
| 更早的使用 | 法国工程师埃米尔·博多（Émile Baudot）于 1878 年演示的电报机中使用了格雷码<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup> |
| 二进制到格雷码的转换 | \( g = b \oplus (b \gg 1) \)；逆变换是前缀异或，可在对数并行时间内计算<sup>[4](https://aggregate.ee.engr.uky.edu/MAGIC/)</sup> |
| 图论解释 | n 位格雷码对应于 n 维超立方体上的哈密顿回路<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup> |
| 主要应用 | 绝对式旋转编码器、闪速模数转换器的亚稳态抑制、异步时钟域跨越，以及卡诺图标注<sup>[3](https://www.ia.omron.com/support/faq/answer/34/faq00941/)</sup><sup> • </sup><sup>[5](https://www.analog.com/en/resources/technical-articles/2022/07/16/07/24/understanding-flash-adcs.html)</sup><sup> • </sup><sup>[6](https://docs.amd.com/r/en-US/ug574-ultrascale-clb/Asynchronous-Clock-Domain-Crossing)</sup> |

## 单比特特性

在普通二进制计数中，一次进位可能同时翻转多个比特：从 3 变为 4 时，011 变为 100，一下子翻转了三个比特。<sup>[7](https://www.ituonline.com/tech-definitions/what-is-gray-code/)</sup> 如果读取者在多个比特正在跳变时采样，就可能捕获到新旧值的混合，产生一个与两者都相差甚远的数字。使用格雷码时，永远只有一个比特处于不确定状态，因此在任何瞬间读取的值要么是旧值，要么是新值，绝不会是第三个值。<sup>[8](https://control.com/technical-articles/an-introduction-to-gray-code-for-encoders)</sup> 根据美国国家标准与技术研究院的《算法与数据结构词典》，这对机械编码器尤为重要：在普通二进制编码下，相邻值之间最多可能有 n 个比特翻转，因此传感元件之间的微小错位就可能导致严重错误的读数。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup>

## 二进制反射格雷码

以格雷命名的构造是递归的。将 n 位列表记作 \( \mathcal{C}(n) \)，规则为

\[ \mathcal{C}(n) = \mathcal{C}(n-1) \cdot 0 \; \circ \; \overline{\mathcal{C}(n-1)} \cdot 1, \]

其中点号表示在每个字符串后追加一个比特，圆圈表示连接两个列表，上划线表示反转列表；\( \mathcal{C}(0) \) 为空字符串。<sup>[9](https://encyclopediaofmath.org/index.php?title=Gray_code)</sup> 由此得到 \( \mathcal{C}(1) = 0, 1 \)，\( \mathcal{C}(2) = 00, 10, 11, 01 \)，以及 \( \mathcal{C}(3) = 000, 100, 110, 010, 011, 111, 101, 001 \)。<sup>[9](https://encyclopediaofmath.org/index.php?title=Gray_code)</sup>

两种表示之间的转换代价很低。第 K 个格雷码字为 \( K \oplus (K \gg 1) \)，其中 \( \oplus \) 为按位异或。<sup>[4](https://aggregate.ee.engr.uky.edu/MAGIC/)</sup> 从格雷码回到二进制需要依次进行一系列异或运算，等价于并行前缀异或；对于 32 位值，需要五步移位与异或（移位量分别为 16、8、4、2 和 1）。<sup>[4](https://aggregate.ee.engr.uky.edu/MAGIC/)</sup> 对于二进制数与某些格雷码之间的一般转换，存在多项式时间算法。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup>

这种编码不是加权的：与二进制不同，比特的列不带有隐含的位值，因此将格雷码字当作二进制数读取没有直接的算术意义。<sup>[10](https://www.allaboutcircuits.com/technical-articles/gray-code-basics/)</sup> 接收格雷编码位置数据的控制器会先将其转换为二进制再进行运算。<sup>[8](https://control.com/technical-articles/an-introduction-to-gray-code-for-encoders)</sup>

## 历史

单比特变化的思想早于格雷的专利。法国工程师埃米尔·博多于 1878 年演示的电报机中使用了格雷码，而博多早在 1874 年就为其快速电报机取得了专利。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup><sup> • </sup><sup>[11](https://baudotsystems.co/blog/what-the-telegraph-knew/)</sup> 格雷 1947 年的专利申请涉及脉冲编码传输，其中对消息的瞬时幅度进行采样，每个样本被转换为一组通断脉冲。<sup>[2](https://patents.google.com/patent/US2632058A/en)</sup> 该专利陈述的目标是减少编码错误：在传统二进制编码掩模下，电子束从一行孔洞偏移到上一行或下一行的小幅漂移可能产生远大于束偏转误差的编码错误，而重新排列的编码使误差永远不大于偏转误差。<sup>[2](https://patents.google.com/patent/US2632058A/en)</sup> 该专利还主张掩模制造更简单，因为最低有效列的孔洞可以做得比二进制掩模大一倍。<sup>[2](https://patents.google.com/patent/US2632058A/en)</sup>

## 应用

**旋转编码器。** 在绝对式编码器中，带有明暗标记图案的码盘由一排 8 到 10 个传感器读取，控制器必须同时读取所有传感器才能获得位置。<sup>[8](https://control.com/technical-articles/an-introduction-to-gray-code-for-encoders)</sup> 如果码盘图案采用普通二进制，多个传感器可能同时跳变，错时读取可能返回半新半旧的值，被解释为运动到了远处的位置；使用格雷码时，控制器只能读到单比特变化之前或之后的值。<sup>[8](https://control.com/technical-articles/an-introduction-to-gray-code-for-encoders)</sup> 编码器制造商欧姆龙以同样的方式总结其优势：由于只有一个比特变化，无需读取定时选通信号，与二进制或 BCD 相比编码读取稳定。<sup>[3](https://www.ia.omron.com/support/faq/answer/34/faq00941/)</sup> 正交编码器——旋钮上常见的两通道增量式编码器——本身就是一种 2 位格雷码，这就是为什么真正的正交输出从不会在同一瞬间切换两个通道。<sup>[12](https://industrialmonitordirect.com/blogs/knowledgebase/troubleshooting-incremental-encoder-quadrature-signal-errors)</sup>

**模数转换。** 在闪速模数转换器中，比较器输出可能不确定，这种情况称为亚稳态；格雷码编码只允许输出中 1 个比特同时变化，可以极大地改善亚稳态问题。<sup>[5](https://www.analog.com/en/resources/technical-articles/2022/07/16/07/24/understanding-flash-adcs.html)</sup> 后来的专利构建了额外免受气泡错误影响的编码器，气泡错误是指虚假的比较器模式通过编码器。<sup>[13](https://patents.google.com/patent/US6388602B1/en)</sup>

**时钟域跨越。** 当计数器必须由运行在不同异步时钟上的逻辑读取时，多比特总线的每个比特都面临亚稳态捕获的风险。对计数器进行格雷编码意味着目标域通过寄存器同步器采样时，只能看到旧的或新的计数值，绝不会看到混合值；对于缺乏这种结构的一般多比特总线，推荐的做法是使用独立时钟（双时钟）FIFO。<sup>[6](https://docs.amd.com/r/en-US/ug574-ultrascale-clb/Asynchronous-Clock-Domain-Crossing)</sup>

**卡诺图。** 卡诺图将真值表排列为网格，其行和列以格雷码顺序标注，使得任何两个相邻单元格——包括跨越左右和上下环绕的单元格——恰好在一个输入变量上不同；这种相邻性正是使相等的乘积项可以被分组并可视化简化的原因。<sup>[14](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c4/c4s1/)</sup><sup> • </sup><sup>[15](https://university.kurz.fyi/computer-science/computer/digital-logic-computer-organization/karnaugh-maps-and-boolean-simplification)</sup>

## 变体与推广

4 位及以上的格雷码并不唯一，即使允许比特的置换或反转也是如此。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup> 完整的 n 位循环码有 \( 2^{n} \) 项，但应用可能需要更少的项；一项专利方法通过从完整反射码的每一半中各选取 \( k \) 个有序项，为任意 \( 2^{n-1} < 2k < 2^{n} \) 生成具有 \( 2k \) 项的循环码。<sup>[16](https://patents.google.com/patent/US6970113B1/en)</sup>

这一思想超越了比特串。组合格雷码这一术语于 1980 年提出，是一类组合对象的列表，每个对象恰好出现一次，相邻对象之间相差一个预先指定的小变化；此类列表已知存在于比特串、组合、排列、划分、三角剖分，以及由图定义的对象（如生成树和完美匹配）中。<sup>[17](https://www.combinatorics.org/ojs/index.php/eljc/article/view/ds26)</sup> 在图论中，n 位格雷码对应于 n 维超立方体上的哈密顿回路，而蛇形码和螺旋形码是与格雷码相关的结构，被研究用于单比特错误检测。<sup>[1](https://xlinux.nist.gov/dads/HTML/graycode.html)</sup><sup> • </sup><sup>[10](https://www.allaboutcircuits.com/technical-articles/gray-code-basics/)</sup> 数据总线的格雷编码也被应用于减少片上网络互连中的开关噪声和串扰。<sup>[18](https://technav.ieee.org/topic/reflective-binary-codes/)</sup>

## References

1. [Gray code, NIST Dictionary of Algorithms and Data Structures](https://xlinux.nist.gov/dads/HTML/graycode.html)
2. [Frank Gray, "Pulse Code Communication", U.S. Patent 2,632,058, Google Patents](https://patents.google.com/patent/US2632058A/en)
3. [FAQ00941: What is the feature of the Gray code?, OMRON Industrial Automation](https://www.ia.omron.com/support/faq/answer/34/faq00941/)
4. [The Aggregate Magic Algorithms, University of Kentucky](https://aggregate.ee.engr.uky.edu/MAGIC/)
5. [Understanding Flash ADCs, Analog Devices](https://www.analog.com/en/resources/technical-articles/2022/07/16/07/24/understanding-flash-adcs.html)
6. [Asynchronous Clock Domain Crossing, UG574, AMD](https://docs.amd.com/r/en-US/ug574-ultrascale-clb/Asynchronous-Clock-Domain-Crossing)
7. [What Is Gray Code?, ITU Online IT Training](https://www.ituonline.com/tech-definitions/what-is-gray-code/)
8. [An Introduction to Gray Code for Encoders, control.com Technical Articles](https://control.com/technical-articles/an-introduction-to-gray-code-for-encoders)
9. [Gray code, Encyclopedia of Mathematics](https://encyclopediaofmath.org/index.php?title=Gray_code)
10. [Gray Code Basics, All About Circuits Technical Articles](https://www.allaboutcircuits.com/technical-articles/gray-code-basics/)
11. [What the telegraph knew, Baudot Systems](https://baudotsystems.co/blog/what-the-telegraph-knew/)
12. [Fix Incremental Encoder A/B Channel Simultaneous Edge Errors, Industrial Monitor Direct](https://industrialmonitordirect.com/blogs/knowledgebase/troubleshooting-incremental-encoder-quadrature-signal-errors)
13. [US6388602B1: Bubble and meta-stability error immune gray-code encoder, Google Patents](https://patents.google.com/patent/US6388602B1/en)
14. [Computation Structures, Lecture 4.1 annotated slides, MIT OpenCourseWare](https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/c4/c4s1/)
15. [Karnaugh Maps and Boolean Simplification, Digital Logic & Computer Organization](https://university.kurz.fyi/computer-science/computer/digital-logic-computer-organization/karnaugh-maps-and-boolean-simplification)
16. [US6970113B1: Method for generation of even numbered reduced Gray codes, Google Patents](https://patents.google.com/patent/US6970113B1/en)
17. [Torsten Mütze (2023). Combinatorial Gray Codes — an Updated Survey. The Electronic Journal of Combinatorics, Dynamic Survey DS26.](https://www.combinatorics.org/ojs/index.php/eljc/article/view/ds26)
18. [Reflective binary codes, IEEE Technology Navigator](https://technav.ieee.org/topic/reflective-binary-codes/)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Redundant and modular computer number systems

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/gray-code

English original: https://www.edgepedia.org/gray-code
