Back to tree

➖ 补码:让电脑做减法的聪明办法➖ Two's Complement: Subtraction by Adding

上一片里,字符住进了数字。可数字还有一半没安家:负数怎么办?更麻烦的是,💻 小电脑里根本没有减法器! Last leaf, characters moved into numbers. But half the numbers are still homeless: what about negatives? Worse — 💻 the little computer has no subtractor at all!

第 1 步:没有减法器,就把减法变成加法Step 1: No subtractor? Turn subtraction into addition

加法器是小电脑唯一的算术机器,那就让减法来迁就它。The adder is the little computer's only arithmetic machine — so subtraction must adapt to it.

5-3=5+(-3):加法器就够了
5 - 3 = 5 + (-3):把 5 和 -3 两张卡喂进加法器,结果一样是 2。所以问题只剩一个:负数 -3 该怎么存? 5 − 3 = 5 + (−3): feed the cards 5 and −3 into the adder and out comes 2 all the same. So only one question remains: how do we store a negative like −3?

负数挤不进 0~255 的小格子?先看看格子转满一圈会发生什么。Negatives can't squeeze into cells of 0–255? First watch what happens when the cells spin one full turn.

第 2 步:8 位小转盘,转满就回到 0Step 2: The 8-bit dial spins full circle back to 0

255+1=0:转满一圈回绕
8 个格子最多数到 255,再加 1 就转满一圈回到 0。这不是故障,是约定:多出来的进位直接丢掉,有限的格子循环用。 Eight cells count up to 255 at most — plus one more spins full circle back to 0. That's not a bug, it's the deal: the extra carry is dropped, and the small set of cells loops forever.

回绕有什么用?想想:255 的上一个邻居是 0,它当 -1 用合不合适?What's the wrap good for? Think: 255 sits right before 0 — couldn't it play the role of −1?

第 3 步:负数住进补码Step 3: Negatives move into complements

-1 存成 11111111
从 0 往回倒:0 的上一个 255 当 -1,再上 254 当 -2。8 位里 -1 存成 11111111,-3 存成 253。5 - 3 就变成 5 + 253 = 258,回绕成 2。原码拿首位当符号,反码其余位取反,补码是反码再加 1。只有补码能让加法器直接算对。 Count backwards from 0: the cell before 0, 255, plays −1; next 254 plays −2. In 8 bits, −1 is stored as 11111111 and −3 as 253. So 5 − 3 becomes 5 + 253 = 258, wrapping to 2. Sign-magnitude spends its first bit on the sign, ones' complement flips the rest, two's complement adds one more — and only two's complement lets the adder get it right directly.

🎮 你来当操作员(30 秒)🎮 Your turn: adder operator (30 seconds)

拖滑块调被加数 A,再押一把会不会回绕,带小电脑连过 4 关。Drag the slider to set addend A, bet on wrap or not, and walk the little computer through 4 rounds.

一句话记住它:补码 = 给负数办的加法器通行证,减法全变成加法。 Remember it in one line: two's complement is the adder pass issued to negatives — every subtraction becomes an addition.
减法变加法:a - b = a + (-b),加法器一台就够Subtraction becomes addition: a − b = a + (−b) — one adder is enough 负数存补码:-1 存成 255(11111111),溢出回绕 255+1=0Negatives as complements: −1 stored as 255 (11111111); overflow wraps 255+1=0 原码管符号、反码取反、补码再加 1,只有补码能直接加Sign-magnitude marks the sign, ones' complement flips, two's complement adds one — only it adds directly

← 浏览全部 358 个知识点← Browse all 358 topics

内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。 Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.