# 反码

**反码**是一种用二进制表示有符号整数的方法：正数按普通二进制数存储，负数则将每一位取反。对于一个 n 位字，符号位的权值为 −(2^(n−1) − 1)，而非单纯的符号标志；该体系是缩减基数补码的二进制情形，即相对于一串 1 而非 2 的幂取补。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup><sup> • </sup><sup>[2](https://web.ece.ucsb.edu/Faculty/Parhami/pubs_folder/parh02-arith-encycl-infosys.pdf)</sup>

| 关键事实 | 细节 |
|---|---|
| 取负 | −X 是 X 的按位取反；对该操作施加两次即还原为原值。<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> |
| 两个零 | 全 0 和全 1 的位串都表示零，因此 n 位系统只有 2^N − 1 个不同值，而非 2^N 个。<sup>[4](https://mathworld.wolfram.com/OnesComplement.html)</sup><sup> • </sup><sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> |
| 范围 | −(2^(N−1) − 1) 到 +(2^(N−1) − 1)：16 位时为 −32767 到 +32767，负端比补码少一个值。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> |
| 加法代价 | 需要循环进位；使用补码部件的实现可能需要二次处理。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup> |
| 历史上的机器 | CDC 6600、LINC、PDP-1、UNIVAC 1107 和 1108；CDC 的后继机型一直使用到 1980 年代末。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup><sup> • </sup><sup>[7](https://bitsavers.org/pdf/univac/1100/1108/UP-4046r3_UNIVAC_1108_System_Description_1970.pdf)</sup> |
| 现存遗迹 | 互联网校验和（RFC 1071、RFC 791）是 16 位反码求和，Unisys ClearPath 系统仍在模拟反码架构。<sup>[8](https://www.rfc-editor.org/rfc/inline-errata/rfc1071.html)</sup><sup> • </sup><sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup> |
| 标准地位 | C23 移除了补码之外的有符号整数表示，终结了 C 语言长期以来对反码的允许。<sup>[9](https://www.cppreference.com/c/23)</sup> |

## 定义与表示

在 n 位反码系统中，非负数写作普通的无符号二进制数，而一个数的负数通过将每一位取补得到。在二进制中，数位取补就是简单的布尔非运算，因此取负就是对整个字按位取反，无需加法步骤。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup><sup> • </sup><sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> 对取反施加两次即恢复原模式，因此取负是其自身的逆运算。<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup>

该体系符合补码数系统的一般理论。选择一个至少不小于 N + P + 1 的补码常数 M，即可让编码表示 [−N, P] 范围内的有符号整数；反码是 M = 2^k − 1 的特例，即基数为 2 的缩减基数补码。<sup>[2](https://web.ece.ucsb.edu/Faculty/Parhami/pubs_folder/parh02-arith-encycl-infosys.pdf)</sup> 在这一方案中，对于 n 位字，符号位的权值为 −(2^(n−1) − 1)，而在补码中其权值为 −(2^(n−1))，在原码中符号位仅对数值取负。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup> 十进制中的类比是九的补码：在三位十进制系统中，零的补码是 999 − 000 = 999，因此零被表示为 000 和 999 两种形式。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup>

名称中撇号的位置是有意义的。补码数（two's complement）是相对于 2 的单个幂取补，而反码数（ones' complement）是相对于一长串 1 取补。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup> 早在 1959 年，计算机文献就将这两类加以区分：相对于 b^n + 1 取补（十的补码或补码）与相对于 b^n + 1 − b − 1 取补（九的补码或反码），并指出二者在算术单元设计上各有优劣。<sup>[10](https://bitsavers.org/pdf/brl/Smith_-_Electronic_Digital_Computers_1959.pdf)</sup>

## 算术：循环进位

反码加法需要一个称为循环进位的修正步骤：每当最高有效位产生进位输出时，该进位必须加回到最低有效位。<sup>[4](https://mathworld.wolfram.com/OnesComplement.html)</sup> 其原因在代数中清晰可见。对于正数 P 和负数 N，原始无符号和为 S = P + (2^n − 1 − |N|)；该和的低 n 位给出结果，只有当产生超出 n 位字的进位时，才将该进位加回最低有效位。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup><sup> • </sup><sup>[15](https://www.cse.iitd.ac.in/~aseth/col100-2021/5-numbersystems.pdf)</sup> RFC 1071 以操作方式陈述了同一规则：在补码机器上，反码和必须借助循环进位来计算，即最高有效位的溢出被加到最低有效位。<sup>[8](https://www.rfc-editor.org/rfc/inline-errata/rfc1071.html)</sup>

这一修正有硬件代价。循环进位需要额外的进位处理。在使用补码部件的实现中，符号位的进位输出必须反馈回来，需要二次处理。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup> 用补码部件实现反码加法器的硬件设计者需要两个加法器或每次加法两个周期：先执行求和，将高位进位输出传播到进位输入，再执行第二次求和。<sup>[11](https://www.ietf.org/rfc/rfc1936.txt)</sup> 相比之下，溢出检测与补码完全相同：OVERFLOW = Cn−1 ⊕ Cn，即进入和离开符号位的进位的异或。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup>

## 两个零的问题

由于取负是按位取反，对零的表示取反得到全 1 串，因此全零和全 1 位串都表示零。<sup>[4](https://mathworld.wolfram.com/OnesComplement.html)</sup> 在三位中，这两种表示是 000 和 111。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> 这损失了一点容量：N 位系统有 2^(N−1) 个正表示（包括 +0）和 2^(N−1) 个负表示（包括 −0），共 2^N − 1 个不同值。<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup>

双零造成实际麻烦。零检测变得更困难，需要同时使用 N 位或门和 N 位与门来捕获两种编码。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> 将一个反码数与其相反数相加，结果是零的全 1 表示（负零），而非全 0 表示。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> 在 C 标准的传统中，符号位为 1 且所有值位为零的表示称为负零，它既可以是陷阱表示，也可以是正常值，这种模糊性塑造了数十年的实现定义行为。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup>

## 数据对比

三种经典有符号表示之间的定量比较显示了反码所处的位置：

| 属性（n 位） | 反码 | 补码 | 原码 |
|---|---|---|---|
| 范围 | −(2^(n−1) − 1) 到 +(2^(n−1) − 1)<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> | −2^(n−1) 到 2^(n−1) − 1<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> | 与反码相同<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> |
| 不同值个数 | 2^n − 1<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> | 2^n<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup> | 2^n − 1<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup> |
| 零的编码数 | 2（+0 和 −0）<sup>[4](https://mathworld.wolfram.com/OnesComplement.html)</sup> | 1<sup>[12](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup> | 2<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> |
| 16 位范围 | −32767 到 +32767<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> | −32768 到 +32767<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> | −32767 到 +32767<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> |
| 加法 | 需要循环进位；使用补码部件的实现可能需要二次处理<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup> | 普通加法器<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> | 不能直接使用无符号加法硬件<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup> |

在四位中，原码和反码由于重复的零只提供 15 个不同数字，而补码和移码（偏置码）表示 16 个。<sup>[6](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)</sup>

## 计算机中的历史使用

若干有影响力的早期计算机系列选择了反码。数字设备公司程序数据处理机-1（PDP-1）1961 年的手册指出，负数表示为正数的反码，在 18 位字中第 0 位为符号位（正数为零），第 1 至 17 位为数值位。<sup>[13](https://www.computerhistory.org/pdp-1/_media/pdf/dec.pdp-1.pdp-1_programmed_data_processor-1.1961.102664957.pdf)</sup> UNIVAC 1108 的加法器是一个反码减法加法器，作用于 36 位或 72 位字，当定点加减法期间产生循环进位时，机器会置位进位指示符。<sup>[7](https://bitsavers.org/pdf/univac/1100/1108/UP-4046r3_UNIVAC_1108_System_Description_1970.pdf)</sup> 一份 C++ 标准调查将 CDC 6600、LINC、PDP-1 和 UNIVAC 1107 列入使用该表示的众多早期计算机之中，并指出 CDC 6600 的后继机型一直使用反码直到 1980 年代末。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup>

设计理由在于取负的简便性：要对一个数取负，只需将所有位取反，无需像补码那样加 1。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup>

## 与补码和原码的比较

补码因算术上的理由取代了反码。反码取负容易，但难以执行取负之外的算术运算，而补码用普通加法器即可支持算术运算，无需修正周期。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> 补码还具有单一的零，消除了零检测代价以及全 0 和全 1 零编码之间的区别。<sup>[12](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup><sup> • </sup><sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup> 原码和反码共享相同的对称范围和双零问题，且二者都不能直接使用无符号加法硬件。<sup>[3](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)</sup>

历史的转折来得早且果断。约翰·冯·诺依曼（John von Neumann）在其 1945 年的《EDVAC 报告初稿》中建议使用补码二进制表示；1949 年的 EDSAC 使用了它，IBM 1964 年的 System/360 使补码成为业界的主导表示。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup> 反码保留了一项真正的优势——取负极其简单——但这被额外的进位处理和重复的零所抵消。<sup>[5](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)</sup>

## 在校验和、标准及现状中的遗产

RFC 1071 将互联网校验和定义为数据的 16 位字的 16 位反码和，字节对组成的字表示为 [a,b] = a·256 + b。<sup>[8](https://www.rfc-editor.org/rfc/inline-errata/rfc1071.html)</sup> RFC 791 将 IP 首部校验和定义为首部中所有 16 位字反码和的 16 位反码，计算时校验和字段视为零。<sup>[14](https://stackoverflow.com/questions/161797/is-ones-complement-a-real-world-issue-or-just-a-historical-one)</sup> 校验和从零开始，通过对数据按 16 位块求和并取反码得到；当数据和校验和本身以这种方式求和时，若无错误发生，结果应为零校验和。<sup>[11](https://www.ietf.org/rfc/rfc1936.txt)</sup>

少数系统仍在体现这一表示。Unisys ClearPath 系统提供 ANSI C 编译器，其中有符号类型使用反码表示：signed char 为 9 位（范围 −2⁸+1 到 2⁸−1），signed short 为 18 位，signed int 为 36 位（−2³⁵+1 到 2³⁵−1），signed long long 为 72 位（−2⁷¹+1 到 2⁷¹−1）；这些机器使用 x86 CPU 为拥有遗留应用的客户模拟旧的反码架构。<sup>[14](https://stackoverflow.com/questions/161797/is-ones-complement-a-real-world-issue-or-just-a-historical-one)</sup><sup> • </sup><sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup> 即使在纯补码处理器中，反码算术也出现在指令编码层面：Intel 64 和 IA-32 手册指出，VEX.vvvv 位域以反码形式指定一个寄存器。<sup>[14](https://stackoverflow.com/questions/161797/is-ones-complement-a-real-world-issue-or-just-a-historical-one)</sup>

编程语言的故事已经落幕。C11 允许有符号整数类型有三种表示（原码、补码和反码），选择由实现定义。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup> 在 C23 之前，反码或原码表示的可能性使得有符号类型的按位运算行为由实现定义，并使负零及其生成尝试成为未定义行为的来源；C23 严格定义的补码表示消除了这些问题。<sup>[12](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup> C23 还移除了补码实现可能省略最小值的可能性，这此前会导致未定义行为。<sup>[12](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup> C23 将“补码之外的有符号整数表示”列入其移除的特性之中。<sup>[9](https://www.cppreference.com/c/23)</sup> 2018 年的一项调查未发现任何同时使用 C++ 和非补码表示的现代机器，MSVC、GCC 和 LLVM 也都不支持其他表示。<sup>[1](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)</sup>

## References

1. [P0907R0: Signed Integers are Two's Complement, C++ committee paper](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)
2. [Number Representation and Computer Arithmetic, B. Parhami, Encyclopedia of Information Systems](https://web.ece.ucsb.edu/Faculty/Parhami/pubs_folder/parh02-arith-encycl-infosys.pdf)
3. [Signed Int: One's Complement, UMD CMSC 311 course notes](http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/onescomp.html)
4. [Ones' Complement, Wolfram MathWorld](https://mathworld.wolfram.com/OnesComplement.html)
5. [AN 83: Binary Numbering Systems, Altera Corporation](https://docs.altera.com/api/khub/documents/VlP44CmsablTdTRYkyi4vA/content)
6. [Computer Arithmetic, lecture notes, Stanford](http://arith.stanford.edu/~hfahmy/webpag_es/arith_class/arith.pdf)
7. [UP-4046r3 UNIVAC 1108 System Description (1970), bitsavers](https://bitsavers.org/pdf/univac/1100/1108/UP-4046r3_UNIVAC_1108_System_Description_1970.pdf)
8. [RFC 1071: Computing the Internet checksum, IETF](https://www.rfc-editor.org/rfc/inline-errata/rfc1071.html)
9. [C23, cppreference.com](https://www.cppreference.com/c/23)
10. [Electronic Digital Computers, Smith, 1959, bitsavers](https://bitsavers.org/pdf/brl/Smith_-_Electronic_Digital_Computers_1959.pdf)
11. [RFC 1936: Implementing the Internet Checksum in Hardware, IETF](https://www.ietf.org/rfc/rfc1936.txt)
12. [C issue 1067: Bitwise operators on signed types, WG14](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)
13. [Programmed Data Processor-1 Manual, 1961, Computer History Museum](https://www.computerhistory.org/pdp-1/_media/pdf/dec.pdp-1.pdp-1_programmed_data_processor-1.1961.102664957.pdf)
14. [Is one's complement a real-world issue, or just a historical one? Stack Overflow](https://stackoverflow.com/questions/161797/is-ones-complement-a-real-world-issue-or-just-a-historical-one)
15. [cse.iitd.ac.in](https://www.cse.iitd.ac.in/~aseth/col100-2021/5-numbersystems.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 19, 2026; Sep 30, 2026; 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/ones-complement

English original: https://www.edgepedia.org/ones-complement
