| English | 释义 | 拼音 |
|---|---|---|
| overflow/ˌəʊvəˈfləʊ/ | 溢出 | yì chū |
| two's complement/tuːz ˈkɒmplɪmənt/ | 补码 | bǔ mǎ |
| register/ˈredʒɪstə/ | 寄存器 | jì cún qì |
| sign bit/saɪn bɪt/ | 符号位 | fú hào wèi |
| most significant bit/məʊst sɪɡˈnɪfɪkənt bɪt/ | 最高有效位 | zuì gāo yǒu xiào wèi |
| one's complement/wʌnz ˈkɒmplɪmənt/ | 反码 | fǎn mǎ |
| Binary Coded Decimal/ˈbaɪnəri ˈkəʊdɪd ˈdesɪml/ | 二进制编码十进制 | èr jìn zhì biān mǎ shí jìn zhì |
丢了一半的指数
- 1983 年 9 月 1 日,温哥华证券交易所指数停在 524.811。它在二十二个月前从 1000 开盘,而市场一路上涨。
- 程序在每笔交易后重算指数,每次都截断而不是四舍五入。每次截断丢掉零点几分。每天三千笔交易完成了其余的工作。
- 正确重算后,指数是 1098.892:把它腰斩的是算术,不是市场。
- 固定位数上的算术不是你在学校学的算术。这一课讲二进制加法、溢出(overflow)、用补码(two's complement)做减法、有符号整数,以及 BCD。
二进制加法
- 从右往左逐列相加,向下一列进位,和十进制完全一样。
- 规则:$0 + 0 = 0$;$0 + 1 = 1$;$1 + 1 = 10$,写 0 进 1;$1 + 1 + 1 = 11$,写 1 进 1。
0101$+$0011$=$1000,也就是 $5 + 3 = 8$。

方法和十进制一样,只是只有两个数字要进位
Binary & signed integers
byte = Σ place values
See how an 8-bit pattern maps to a number (and how it would overflow past 255).
Add the binary numbers 0101 + 0011. Give the 4-bit result.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$5 + 3 = 8$, which is 1000 in binary.
溢出
- 当计算结果需要的位数超过寄存器(register)能容纳的位数时,就发生溢出。最高位的进位丢失,所以存下的答案是错的。
- 它是寄存器宽度的属性,不是数字 255 的属性:在 16 位寄存器里同样的加法毫无问题。
- 在有符号算术里,标志是符号位错误地翻转:两个正数得出负数,或两个负数得出正数。
Overflow in binary addition means:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Overflow occurs when the sum is too large to fit in the available bits; the carry out of the leftmost column is lost.
例题:正确地说出溢出
- 把 8 位无符号整数
10110101和01101100相加并对结果作出说明。 - 和是
1 0010 0001,需要 9 位。真实结果是 289。 - 完整答案:发生了溢出,因为 289 大于 8 位寄存器能容纳的最大值 255;最高位的进位丢失,所以存下的结果
0010 0001是 33,是错的。 - 得分点在于说出寄存器宽度,并说明结果无法在其中表示。单说"答案大于 255"不得分。
Two 8-bit unsigned integers are added and the result needs 9 bits. Which is the full-mark explanation?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Name the register width, say the result cannot be represented in it, and say what happens to the carry. The other options state a symptom without the cause.
用补码做减法
- 计算 $A - B$:把 $B$ 的每一位取反再加 1,得到它的补码,加到 $A$ 上,然后丢弃最后的进位。
- 8 位下的 $100 - 30$:
0001 1110取反是1110 0001,加 1 得1110 0010。 0110 0100$+$1110 0010$=$1 0100 0110;丢掉最前面的 1,读0100 0110$= 70$。✓

减法变成了加法,这就是处理器不需要减法器的原因
What is the 4-bit two's complement of 0011? (invert, then add 1)
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Invert 0011 → 1100, then add 1 → 1101 (which represents $-3$).
Put the steps of subtracting B from A by two's complement in order.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Invert, add one, add, discard. The discard is what keeps the answer in the register's width.
补码有符号整数
- 在 $n$ 位补码数中,最高有效位(most significant bit)是符号位(sign bit):0 表示正,1 表示负。等价地说,最高位带一个负的位值 $-2^{n-1}$。
- 读一个负数:每一位取反,加 1,再在前面加负号。
1011 0100取反得0100 1011,加 1 得0100 1100$= 76$,所以值是 $-76$。用位值检查:$-128 + 32 + 16 + 4 = -76$。✓ - $n$ 位的范围是 $-2^{n-1}$ 到 $+2^{n-1} - 1$:8 位给出 $-128$ 到 $+127$,12 位给出 $-2048$ 到 $+2047$。最负的值是一个 1 后面全是 0;最正的值是一个 0 后面全是 1。

11111111 一种读法是 255,另一种读法是 −1
Two's complement signed bits
The leftmost bit carries a 负形 place value. Flip any bit — or hit Negate (invert every bit, then add 1) — and watch the signed value change.
Read the 8-bit two's complement number 11111101 as a signed denary value.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
MSB is 1 (negative). Invert → 00000010, add 1 → 00000011 $= 3$, so the value is $-3$.
What is the largest positive value an 8-bit two's complement number can hold?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Range is $-2^{7}$ 到 $2^{7}-1$, i.e. $-128$ 到 $+127$. The maximum is 01111111 = 127.
例题:用 12 位写出 −108
- 从 12 位的 $+108$ 开始:$108 = 64 + 32 + 8 + 4$,所以是
0000 0110 1100。 - 每一位取反:
1111 1001 0011。加 1:1111 1001 0100。 - 用位值检查,最高位值 $-2048$:$-2048 + 1024 + 512 + 256 + 128 + 16 + 4 = -108$。✓
- 最常见的错误是符号加数值:把最高位设为 1、其余不动。那是另一种更老的方案,在这里零分。
Write −108 as a 12-bit two's complement number (spaces allowed).
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
+108 is 0000 0110 1100; invert to 1111 1001 0011 and add 1. Check: −2048 + 1024 + 512 + 256 + 128 + 16 + 4 = −108.
反码
- 反码(one's complement)是更老的方案:把正数的每一位取反就得到负数,没有加 1 这一步。$+30$ 是
0001 1110,所以 $-30$ 是1110 0001。 - 它的缺点是两个零,
0000 0000和1111 1111,浪费一个位模式,也让算术变得别扭。 - 补码只有一个零,而且让同一个加法器电路就能做减法,这就是它胜出的原因。
One's complement is preferred to two's complement because it has only one representation of zero.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The opposite: one's complement has two zeros, +0 and −0. Two's complement has one, and lets the adder do subtraction.
二进制编码十进制
- 在 二进制编码十进制(Binary Coded Decimal,BCD)中,每个十进制数位存成它自己的 4 位模式,只用
0000到1001。 - 93 的 BCD 是
1001 0011,这与纯二进制的 930101 1101不同。把一个当成另一个读是考试最爱的陷阱。 - 用途:计算器、数字钟和七段显示器,每个数位单独驱动;还有货币,BCD 避免了纯二进制存 $0.1$ 的舍入误差。代价是浪费了位模式,因为
1010到1111从不使用。

一个数位,一个半字节,一个显示位
Write the denary digit $9$ as a 4-bit BCD pattern.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$9$ is(是) 1001. In BCD each denary digit gets its own nibble (0000–1001).
Match each bit pattern to what it represents.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Nothing in the bits says how to read them. The agreed representation decides the value.
容易丢掉的分
- 用题目给的寄存器宽度解释溢出,不要说"它大于 255"。
- 取负要取反再加 1。把最高位设为 1 是符号加数值,是另一种方案。
- 补码减法以丢弃最后的进位结束。留着它会得到九位的答案。
- BCD 分别存每个数位;纯二进制存整个数。
1001 0011在 BCD 里是 93,在二进制里是 147。
你掌握了
- 带进位逐列相加;溢出是结果需要的位数超过寄存器容量,答案要点出那个宽度
- 减法靠加补码——取反加 1——然后丢弃最后的进位
- 有符号:MSB 是符号位,带 $-2^{n-1}$;范围 $-2^{n-1}$ 到 $+2^{n-1}-1$;读负数靠取反、加 1、再取负
- 反码只取反,有两个零;BCD 把每个十进制数位存进自己的半字节,用于钟表、计算器和货币