同一串 bits 为什么能有不同含义?
电脑的存储器保存的是 bits。程序必须约定怎样解释这些 bits:它们可能代表一个正整数、负整数、hexadecimal value、BCD digits,甚至是一个字符。本章的核心不是只做进制转换,而是理解representation 决定 bits 的含义。
| 考纲范围 | 学生最终需要做到 |
|---|---|
| Binary magnitudes | 区分 binary prefixes 与 decimal prefixes,并使用 kibi/kilo、mebi/mega、gibi/giga、tebi/tera。 |
| Number systems | 使用 binary、denary、hexadecimal、BCD、one’s complement 和 two’s complement。 |
| Conversion | 在不同 number base 或 representation 之间转换整数。 |
| Arithmetic | 进行正数和负数的 binary addition 与 subtraction,并解释 overflow。 |
| Applications | 描述 BCD 与 hexadecimal 的实际用途,并解释选择理由。 |
| Character data | 解释字符怎样以内部 binary form 保存,并比较 ASCII、extended ASCII 和 Unicode。 |
Digit 是写在数字中的一个符号;place value 是该位置代表的权值。相同的 digits 放在不同位置,价值不同。
bit、byte 与 binary place value
Bit 是 binary digit,只能是 0 或 1。8 bits 组成 1 byte。硬件使用两种稳定状态表示它们,例如低电压与高电压。
Denary 是 base 10,每向左一位乘以 10;binary 是 base 2,每向左一位乘以 2。8-bit unsigned binary 的位权如下:
01011010 = 64 + 16 + 8 + 2 = 90。Unsigned binary 的范围
n bits 可产生 2^n 个不同组合。Unsigned representation 从 0 开始,所以最大值是 2^n - 1。
Binary prefixes 与 decimal prefixes
存储容量中常见两套单位。Decimal prefix 每一级按 1000 变化;binary prefix 每一级按 1024,也就是 2¹⁰ 变化。
| Binary prefix | Bytes | Decimal prefix | Bytes |
|---|---|---|---|
| kibibyte (KiB) | 2¹⁰ = 1024 | kilobyte (kB) | 10³ = 1000 |
| mebibyte (MiB) | 2²⁰ | megabyte (MB) | 10⁶ |
| gibibyte (GiB) | 2³⁰ | gigabyte (GB) | 10⁹ |
| tebibyte (TiB) | 2⁴⁰ | terabyte (TB) | 10¹² |
Kibi 是 binary prefix,但 mega 是 decimal prefix。题目可能故意跨级比较,例如 kibibyte 与 megabyte,不能只看首字母。
Binary、denary 与 hexadecimal 转换
Hexadecimal 是 base 16,使用 digits 0–9 和 A–F。其中 A=10、B=11、…、F=15。
| Denary | Binary | Hex | Denary | Binary | Hex |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 |
| 1 | 0001 | 1 | 9 | 1001 | 9 |
| 2 | 0010 | 2 | 10 | 1010 | A |
| 3 | 0011 | 3 | 11 | 1011 | B |
| 4 | 0100 | 4 | 12 | 1100 | C |
| 5 | 0101 | 5 | 13 | 1101 | D |
| 6 | 0110 | 6 | 14 | 1110 | E |
| 7 | 0111 | 7 | 15 | 1111 | F |
Binary ↔ hexadecimal
一个 hexadecimal digit 正好对应 4 bits。Binary 从右向左每四位分组:
Hexadecimal → denary
Hex 的位权是 …, 16², 16¹, 16⁰:
Minimum number of bits
选择满足 2^n ≥ number of possible values 的最小 n。例如 256 种状态需要 8 bits;但数值 256 本身需要 9-bit unsigned binary。
BCD:每个 denary digit 单独编码
Binary Coded Decimal 不把整个数直接转换成 pure binary。它把每一个 denary digit 分开,并分别用 4 bits 表示。
108 是 0001 0000 1000。Pure binary 的 108 才是 01101100。4-bit BCD 只使用 0000 到 1001 表示 0 到 9;1010 到 1111 不代表有效的单个 decimal digit。
先把十进制数整体转换为 binary,得到的是 pure binary,不是 BCD。看到 BCD 时必须先拆开十进制 digits。
One’s complement 与 two’s complement
最高位为 1 并不自动表示负数。只有题目说明采用 one’s complement 或 two’s complement 时,bits 才按对应规则解释。
One’s complement
固定 bit length 后,把每个 bit invert:0 变 1,1 变 0。
Two’s complement
先写出正数,并保持题目要求的 bit length。
Invert every bit。
Add 1。
把 two’s complement 转回 denary
可把最左边的 bit 视为负权值。8-bit two’s complement 的位权是:
n-bit two’s complement 范围为 −2^(n−1) 到 2^(n−1)−1。8-bit 范围是 −128 到 127。
Binary addition 与 subtraction
加法规则
从最右位开始逐列相加。若一列的结果有两位,右边一位写在本列,左边一位作为 carry 放到下一列。
用 two’s complement 做减法
A − B 可以改写成 A + (−B):先求 B 的 two’s complement,再进行 binary addition。超过固定 bit length 的最高位 carry 可丢弃。
Overflow:答案超出可表示范围
Overflow 不是“算错了”,而是正确的数学答案不能用当前可用的 bits 表示。8-bit unsigned 只能表示 0–255;8-bit two’s complement 只能表示 −128–127。
Carry 是最高位产生的进位;overflow 取决于当前 representation 的范围。判断时必须先知道数据是 unsigned 还是 two’s complement。
为什么实际系统使用 BCD 与 hexadecimal?
BCD applications
| Application | Matching justification |
|---|---|
| Financial / banking calculations | Monetary values can be represented exactly,避免 normal binary 中某些 decimal fractions 的 accumulating / rounding errors。 |
| Electronic displays,例如 calculator、digital clock | 设备显示 individual denary digits;BCD 与 denary digits 之间转换 straightforward。 |
| Date and time in a PC BIOS | 与人类使用的 denary date/time digits 转换容易。 |
| Barcode systems | 每个 denary digit 可直接、准确地单独编码。 |
Hexadecimal applications
- HTML colour codes,例如
#FC238A。 - Memory addresses、assembly language 与 machine code。
- MAC addresses 与 IPv6 addresses。
Hexadecimal 比长串 binary 更短,更方便人类阅读、记忆和抄写。每个 hex digit 直接对应 4 bits,所以转换 straightforward。
只写 “BCD is used in banking” 通常只能获得 application 分。第二分必须说明为什么该 representation 适合这个场景。
Character sets:用数字表示字符
Character set 是电脑可以表示的全部 characters / symbols 的集合,并且每个 character 都有一个 unique numeric or binary code。
unique codeunique codeunique codeunique codeunique code| Character set | 课堂理解 |
|---|---|
| ASCII | 通常描述为 7 bits,可表示 128 个 codes;主要覆盖 Latin letters、digits、punctuation 和 control characters。 |
| Extended ASCII | 8 bits,可表示 256 个 codes。 |
| Unicode | 提供更大的 code range,可表示更多语言、symbols 和 emoji;ASCII characters 包含在 Unicode 中。 |
学生不需要背诵字母 A 的具体 binary code。考试重点是 unique code、stored in sequence,以及不同 character sets 的范围差异。
完成本章后的检查表
- 我能用 place values 把 unsigned binary 转换成 denary。
- 我能区分 binary prefixes 和 decimal prefixes。
- 我能在 binary、denary 与 hexadecimal 之间转换。
- 我知道 BCD 是 each denary digit separately represented by four bits。
- 我能保持固定 bit length,完成 one’s 和 two’s complement。
- 我能显示 carries 或 two’s complement working,完成 binary addition/subtraction。
- 我能用 representable range 或 number of bits available 解释 overflow。
- 我能给出 BCD/hex application,并写出与场景匹配的 justification。
- 我能定义 character set,并解释一个 word 怎样以 codes in sequence 保存。
- 我能比较 ASCII、extended ASCII 和 Unicode,而不依赖背具体 codes。
进入 1.1 filtered practice,在不看教材的情况下完成更多真题。计算题必须保留 working,简答题按 marks 写独立得分点。
本页练习输入会保存在当前浏览器的 local storage,不会上传到服务器。
课程依据:Cambridge International AS & A Level Computer Science 9618 syllabus for examination in 2026, section 1.1 Data Representation;以及页面标注的 2022–2025 Past Paper questions 与对应 Mark Schemes。