# 有符号数表示法

有符号数表示法是一种约定，用固定的二进制位模式来编码正整数与负整数，有时还包括零。计算机需要这样的约定，因为一串光秃秃的二进制位本身并没有正负之分；在设计者们尝试过若干种编码之后，补码才成为几乎所有现代硬件中有符号整数的标准。

| 关键事实 | 说明 |
|---|---|
| 主要编码 | 原码、反码与补码；移码（偏置表示法）是第四种方案，用于浮点指数之类的特殊用途<sup>[1](https://www.computerinternals.app/en/foundations/twos-complement)</sup> |
| 零 | 原码与反码各有两种零的编码（+0 与 −0）；补码只有一种<sup>[2](https://faculty.kfupm.edu.sa/COE/mudawar/coe202/lessons/Lesson1_5.pdf)</sup> |
| 补码范围 | n 位时，取值从 \( -2^{n-1} \) 到 \( +2^{n-1} - 1 \)，负值比正值多一个<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup> |
| 取负 | 逐位取反再加 1；该运算做两次即回到原数<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup> |
| 运算硬件 | 补码的加法与减法同无符号运算共用同一个加法器，进位直接丢弃；原码则需要额外的符号逻辑和一个专用减法器<sup>[4](https://electricalflux.com/learn-components/binary-to-twos-complement-hardware-math)</sup><sup> • </sup><sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup> |
| 历史沿革 | 约翰·冯·诺依曼在 1945 年的 EDVAC 报告中提出补码；1949 年的 EDSAC 采用了它，IBM 的 System/360（1964）则使它成为业界占主导地位的表示法<sup>[5](https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html)</sup> |
| 语言标准 | C23 要求有符号整数采用补码；在 C23 之前，反码和原码也是合法的 C 表示法<sup>[6](https://open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup> |

## 原码

原码是最直接的编码：最高有效位是符号位（0 表示正，1 表示负），其余各位以普通的无符号二进制数存放数值大小<sup>[1](https://www.computerinternals.app/en/foundations/twos-complement)</sup>。在 3 位字中，011 是 +3，111 是 −3。

这一方案有两个缺点。零有两种编码，000（+0）和 100（−0），这浪费了可用模式中的一个，并迫使比较硬件把两种不同的位模式当作相等<sup>[7](https://utilitiesbunker.com/tools/signed-representations)</sup>。算术运算的代价也高：把异号的数相加，需要一段逻辑先判断哪个绝对值更大，用大的减去小的，再单独算出结果的符号<sup>[8](https://www.unm.edu/~zbaker/ece238/slides/10.pdf)</sup>。

原码确实得到过实际应用。IBM 700/7000 系列科学计算机使用原码记数法，只有它们的变址寄存器例外，用的是补码<sup>[5](https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html)</sup>。

## 反码

在反码中，负数是对应的正数逐位取反：每个 1 变成 0，每个 0 变成 1。取反两次即还原为原值<sup>[9](https://openstax.org/books/introduction-computer-science/pages/5-3-machine-level-information-representation)</sup>。在 3 位中，−3 是 100，即 011 的取反。

反码保持对称的取值范围，正值与负值的数量相等，但它保留了双重零：000 和 111 都表示零<sup>[2](https://faculty.kfupm.edu.sa/COE/mudawar/coe202/lessons/Lesson1_5.pdf)</sup>。加法可以用单个加法器完成，但最高有效位的进位必须回送回来再加一次，这一操作称为循环进位；这使加法成为两步的过程<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。

许多早期计算机采用了这一方案，包括 CDC 6600、LINC、PDP-1 和 UNIVAC 1107。CDC 6600 的后继机型一直沿用反码到 20 世纪 80 年代末，而 UNIVAC 1100/2200 系列的优利（Unisys）后继产品则把它保留得最久<sup>[5](https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html)</sup>。

## 补码

补码由反码加 1 得到。例如，011（+3）的补码是 100 + 1 = 101，表示 −3<sup>[9](https://openstax.org/books/introduction-computer-science/pages/5-3-machine-level-information-representation)</sup>。等价地，n 位模式 \( z_{n-1} \dots z_0 \) 的值可以用一个带负权的符号位来计算：

\[ w = -z_{n-1} \cdot 2^{n-1} + z_{n-2} \cdot 2^{n-2} + \dots + z_0 \cdot 2^0 \]

所以 1101 计算为 \( -8 + 4 + 1 = -3 \)<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。

这种编码有三个性质，使它成为最终的赢家。第一，零只有唯一一种表示，因此位模式到数值的映射是一一对应的<sup>[2](https://faculty.kfupm.edu.sa/COE/mudawar/coe202/lessons/Lesson1_5.pdf)</sup>。第二，普通的无符号二进制加法对有符号值原样适用：减法通过把减数取负再相加来完成，最高有效位的最终进位直接丢弃<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。因此一个加法器就能同时处理有符号与无符号运算，这使补码在三种方案中硬件成本最低<sup>[4](https://electricalflux.com/learn-components/binary-to-twos-complement-hardware-math)</sup>。第三，腾出来的模式 100…0 成为最负的值，于是 n 位字容纳 \( 2^n \) 个不同的值，覆盖 \( [-2^{n-1}, +2^{n-1}-1] \)；8 位即 −128 到 +127<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。

代价是取值范围不对称，没有与之对应的 +128，而且取负规则对人类来说不如翻转一个符号位那样直观<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。

**溢出与标志位。** 补码加法中的有符号溢出，发生在进入符号位的进位与从符号位出去的进位不同之时；这两个条件可以用一个异或门合并，产生溢出标志<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。在 x86 上，同一次减法会设置进位标志，用作无符号比较的借位指示，而溢出标志则指示有符号运算出了问题，因此一次运算同时服务于两种解释：100 + 50 得到 0x96，作为无符号数 150 是正确的，但作为有符号数则是错误的 −106，且 OF 置位；而 −1 + 1 得到 0，CF 置位但 OF 清零，意味着有符号的答案是对的，尽管无符号结果发生了回绕<sup>[1](https://www.computerinternals.app/en/foundations/twos-complement)</sup>。

**符号扩展。** 把补码数从 8 位加宽到 16 位，方法是把符号位复制到所有新的高位上，这样无需任何转换运算就能保持数值不变<sup>[1](https://www.computerinternals.app/en/foundations/twos-complement)</sup>。

## 采用的历史

在 1945 年关于电子存储程序数字计算机的提案《关于 EDVAC 的报告初稿》（*First Draft of a Report on the EDVAC*）中，约翰·冯·诺依曼（John von Neumann）提出用补码作为有符号数的二进制记数法；1949 年的 EDSAC 借鉴了这份初稿，采用了补码。早期商用的补码机器包括数字设备公司（Digital Equipment Corporation）的 PDP-5 和 1963 年的 PDP-6。1964 年由当时计算机工业的主导者 IBM 推出的 IBM System/360，使补码成为业界使用最广的二进制表示法；第一台小型机、1965 年的 PDP-8 也使用了它，1969 年数据通用（Data General）的 Nova 和 1970 年的 PDP-11 同样如此<sup>[5](https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html)</sup>。

这一转变在语言标准层面如今已基本完成。2018 年一份 C++ 标准提案对该领域做了调查，发现当时仍在使用非补码表示法的机器只有优利的系统，其 ClearPath MCP 系统以原码形式进行算术运算；优利那时已在带有附加 FPGA 的 x86 CPU 上为老客户模拟旧架构。WG14 关于从 C 中移除非补码表示法的意向投票以 14 票赞成、0 票反对、3 票弃权通过<sup>[5](https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html)</sup>。C23 据此将有符号整数定义为补码，而后来的一份 C 委员会问题记录指出，那些在反码和原码尚属合法时留下的条款——例如实现定义的按位运算结果以及负零的可能性——再也不会出现<sup>[6](https://open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup>。

## 比较

| 性质 | 原码 | 反码 | 补码 |
|---|---|---|---|
| 零的编码 | 两种（+0、−0） | 两种（+0、−0） | 一种<sup>[2](https://faculty.kfupm.edu.sa/COE/mudawar/coe202/lessons/Lesson1_5.pdf)</sup> |
| 范围对称性 | 对称 | 对称 | 不对称，多一个负值<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup> |
| 加法硬件 | 需要加法器、减法器和符号逻辑 | 单个加法器，带循环进位 | 单个加法器，进位丢弃<sup>[8](https://www.unm.edu/~zbaker/ece238/slides/10.pdf)</sup><sup> • </sup><sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup> |
| 取负 | 翻转符号位 | 全部取反 | 全部取反再加 1<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup> |
| 现代地位 | 存留于浮点数的符号位 | 在新设计中已淘汰 | C23 要求；当前硬件普遍采用<sup>[6](https://open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup> |

这一比较解释了结果。补码以一点概念上的代价——不对称的范围和不那么显然的取负规则——换来了一一对应的映射，以及完全复用无符号硬件的算术运算；而实践容忍了这种不对称，以换取更简单的机器<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。

## 相关格式

移码（偏置）表示法存储的是数值加一个固定偏移量，因此位模式始终是无符号的；它用于 IEEE 754 浮点数的指数域，在那里若用补码表示指数，会使浮点硬件复杂化<sup>[1](https://www.computerinternals.app/en/foundations/twos-complement)</sup><sup> • </sup><sup>[9](https://openstax.org/books/introduction-computer-science/pages/5-3-machine-level-information-representation)</sup>。有符号有理数的定点表示法在数字信号处理器上很常见，同样把最高有效位留给符号，其余尾数位按固定的二进制小数点定标<sup>[3](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)</sup>。

## References

1. [Two's complement and signed numbers, Computer Internals](https://www.computerinternals.app/en/foundations/twos-complement)
2. [Machine Representation of Numbers, COE 202 Lesson 1.5, KFUPM](https://faculty.kfupm.edu.sa/COE/mudawar/coe202/lessons/Lesson1_5.pdf)
3. [Technical Computer Science, Part 2, Chapter 2, Universität Marburg](https://www.mathematik.uni-marburg.de/~thormae/lectures/ti1/ti_2_2_eng_web.html)
4. [Binary to Two's Complement: Hardware Math and Embedded Debugging, ElectricalFlux](https://electricalflux.com/learn-components/binary-to-twos-complement-hardware-math)
5. [P0907R3: Signed Integers are Two's Complement, C++ standards proposal](https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html)
6. [C issue 1067: Bitwise operators on signed types, WG14](https://open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)
7. [Signed Number Representations, Utilities Bunker](https://utilitiesbunker.com/tools/signed-representations)
8. [A New Kind of Algebra, ECE 238 slides, University of New Mexico](https://www.unm.edu/~zbaker/ece238/slides/10.pdf)
9. [Machine-Level Information Representation, OpenStax Introduction to Computer Science](https://openstax.org/books/introduction-computer-science/pages/5-3-machine-level-information-representation)

---
*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: 物质世界与数学 › 数学与统计学 › 数与代数 › 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/signed-number-representations

English original: https://www.edgepedia.org/signed-number-representations
