📚 Computer Number Systems: Binary, Octal, Hexadecimal | 计算机数制:二进制、八进制、十六进制
Every item stored in a modern computer – from a letter in a text file to the sound in a music clip – is ultimately represented by numbers. Computer circuits are made of switches that can only be fully on or fully off, so the only reliable number system for the machine itself is binary. Because long binary patterns are hard for humans to read, octadecimal and hexadecimal systems were introduced as compact shorthand. This article explains how binary, octal and hexadecimal work, how to convert between them, and where each system is used in real computing.
现代计算机中存储的一切内容——从文本文件中的一个字母到音乐片段中的声音——最终都由数字表示。计算机电路由只有“完全接通”和“完全断开”两种状态的开关组成,因此机器本身唯一可靠使用的数制就是二进制。由于很长的二进制序列难以阅读,人们引入了八进制和十六进制作为紧凑的简写形式。本文将解释二进制、八进制和十六进制如何工作、它们之间如何转换,以及每种数制在实际计算中的应用。
1. Denary Recap and Positional Notation | 十进制回顾与位权记数法
In everyday life we use the denary (decimal) system, which is base 10. It uses ten digits, 0 to 9, and each position in a number carries a place value that is a power of 10. For example, the number 345 means three hundreds, four tens and five units.
日常生活中我们使用十进制,即基数为 10。它使用 0 到 9 共十个数字,数中每一位都带有一个 10 的幂作为位权。例如,数字 345 表示 3 个百、4 个十和 5 个一。
345₁₀ = 3 × 10² + 4 × 10¹ + 5 × 10⁰
This idea of positional notation is the key to understanding every other number system: the value of a digit depends on both the digit itself and the position it occupies.
位权记数法的思想是理解所有其他数制的关键:一个数字的值既取决于它本身,也取决于它所处的位置。
2. Why Computers Use Two States | 为什么计算机只使用两种状态
Inside a computer, data is moved and stored by transistors that behave like tiny switches. Each switch has two stable states, which we usually represent as 0 and 1. A single binary digit is called a bit, and eight bits make one byte.
计算机内部,数据由晶体管传输和存储,这些晶体管就像微小的开关。每个开关有两种稳定状态,我们通常用 0 和 1 表示。一位二进制数称为一个比特(bit),8 个比特组成一个字节(byte)。
Using only two symbols makes hardware simple, reliable and inexpensive. If a system used ten voltage levels, distinguishing them accurately would be much harder and more error-prone. Two clear voltage levels – low for 0, high for 1 – are easy to measure.
只使用两种符号使硬件简单、可靠且成本低。如果系统使用十个电压等级,准确区分它们将困难得多,也更易出错。两个清晰的电压电平——低电平表示 0,高电平表示 1——很容易检测。
3. The Binary Number System | 二进制数制
Binary is base 2, so it uses only the digits 0 and 1. Each position in a binary number represents a power of 2. The right-most position is 2⁰ = 1, then 2¹ = 2, 2² = 4, 2³ = 8, 2⁴ = 16, 2⁵ = 32, 2⁶ = 64, 2⁷ = 128, and so on.
二进制以 2 为基数,因此只使用 0 和 1 两个数字。二进制数中每一位表示 2 的幂:最右边一位是 2⁰ = 1,之后依次是 2¹ = 2、2² = 4、2³ = 8、2⁴ = 16、2⁵ = 32、2⁶ = 64、2⁷ = 128,依此类推。
Take the binary number 101101₂. Reading from the left, the 1s sit in columns worth 32, 8, 4 and 1.
以二进制数 101101₂ 为例。从左向右看,这些 1 分别位于位权为 32、8、4 和 1 的列上。
101101₂ = 1 × 2⁵ + 0 × 2⁴ + 1 × 2³ + 1 × 2² + 0 × 2¹ + 1 × 2⁰ = 45₁₀
Notice that a four-bit group is often called a nibble, and two nibbles form a byte. These names become helpful when we study hexadecimal later.
注意,4 个二进制位组成的小组常称为半字节(nibble),两个半字节组成一个字节。这些名称在稍后学习十六进制时会很有用。
4. Converting Between Binary and Denary | 二进制与十进制之间的转换
To convert binary to denary, write the place values above each bit, then add together every place value that has a 1 under it. For example, 11011₂ has place values 16, 8, 4, 2, 1, so its value is 16 + 8 + 2 + 1 = 27₁₀.
将二进制转换为十进制时,先在每个比特上方写出位权,然后把所有对应位为 1 的位权相加。例如,11011₂ 的位权为 16、8、4、2、1,因此它的值为 16 + 8 + 2 + 1 = 27₁₀。
To convert denary to binary, use the method of repeated division by 2. Record each remainder, then read the remainders from bottom to top. As an example, convert 45₁₀ to binary.
将十进制转换为二进制时,使用“除 2 取余”法。记录每一次的余数,然后从下往上读出余数。例如,将 45₁₀ 转换为二进制。
45 ÷ 2 = 22 remainder 1
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Reading upwards gives 101101₂, which matches the earlier example. The most common mistake is reading the remainders from top to bottom, so always check by converting back.
从下往上读取得到 101101₂,与前面的例子一致。最常见的错误是自上而下读取余数,因此务必通过反向转换来检查。
5. The Octal Number System | 八进制数制
Octal is base 8 and uses the digits 0, 1, 2, 3, 4, 5, 6 and 7. The place values are powers of 8: 8⁰ = 1, 8¹ = 8, 8² = 64, 8³ = 512, and so on. For example, 63₈ means 6 × 8 + 3 × 1 = 51₁₀.
八进制以 8 为基数,只使用 0、1、2、3、4、5、6 和 7 这八个数字。位权是 8 的幂:8⁰ = 1、8¹ = 8、8² = 64、8³ = 512……例如,63₈ 表示 6 × 8 + 3 × 1 = 51₁₀。
Octal was popular on older computer systems because the number 8 is exactly 2³. One octal digit represents three binary bits directly, making octal a convenient shorthand for binary in early mainframes and in UNIX file permissions.
八进制在早期计算机系统中很流行,因为 8 恰好等于 2³。一位八进制数字直接对应三位二进制位,因此八进制在早期大型计算机和 UNIX 文件权限中是二进制的便捷简写。
6. Converting Between Octal and Binary | 八进制与二进制之间的转换
Because 2³ = 8, each octal digit maps exactly to three binary bits. To convert binary to octal, group the bits into sets of three starting from the right-hand end, then replace each group with the corresponding octal digit. If the left-most group has fewer than three bits, pad it with leading zeros.
因为 2³ = 8,所以每一位八进制数字恰好对应三位二进制位。将二进制转换为八进制时,从最右端开始把比特每三个分成一组,然后用相应的八进制数字替换每组。如果最左边一组不足三位,则补前导 0。
101101₂ = 101 | 101 = 5 | 5 → 55₈
To convert octal to binary, expand each octal digit into its three-bit binary equivalent. For instance, the octal digit 3 becomes 011, and the digit 6 becomes 110.
将八进制转换为二进制时,把每一位八进制数字展开为对应的三位二进制。例如,八进制数字 3 变成 011,数字 6 变成 110。
63₈ = 110 | 011 → 110011₂
Always keep the three-bit groups full: 6 = 110, not 11, otherwise the conversion produces the wrong length.
始终保持三位一组完整:6 应写成 110 而不是 11,否则转换结果长度会出错。
7. The Hexadecimal Number System | 十六进制数制
Hexadecimal, or hex, is base 16. It needs sixteen different symbols, so after the digits 0 to 9 it uses the letters A, B, C, D, E and F to represent 10, 11, 12, 13, 14 and 15. The place values are powers of 16: 16⁰ = 1, 16¹ = 16, 16² = 256, and so on.
十六进制以 16 为基数。它需要十六个不同的符号,因此在数字 0 到 9 之后,使用字母 A、B、C、D、E、F 分别表示 10、11、12、13、14、15。位权是 16 的幂:16⁰ = 1、16¹ = 16、16² = 256……
Consider 2F₁₆. The symbol F stands for 15, so the value is 2 × 16 + 15 = 47₁₀. In general terms, a hex number such as 3A7B₁₆ is evaluated by multiplying each digit by the appropriate power of 16 and adding the results.
以 2F₁₆ 为例。符号 F 表示 15,因此其值为 2 × 16 + 15 = 47₁₀。一般来说,像 3A7B₁₆ 这样的十六进制数,需要将每一位数字乘以相应的 16 的幂再求和。
3A7B₁₆ = 3 × 16³ + 10 × 16² + 7 × 16¹ + 11 × 16⁰
Students often forget that A to F are digits in hex, not variables. They must be treated exactly like the digits 0 to 9 when performing arithmetic.
学生经常忘记 A 到 F 在十六进制中是数字而不是变量。进行运算时,它们必须与 0 到 9 一样作为数字处理。
8. Converting Between Hexadecimal and Binary | 十六进制与二进制之间的转换
Because 2⁴ = 16, one hex digit corresponds exactly to four binary bits. To convert binary to hex, group the bits into sets of four starting from the right, padding with leading zeros if necessary, and convert each group to its hex digit.
因为 2⁴ = 16,所以一位十六进制数字恰好对应四位二进制位。将二进制转换为十六进制时,从最右端开始每四位一组,必要时补前导 0,再将每组转换为相应的十六进制数字。
101101₂ = 0010 | 1101 = 2 | D → 2D₁₆
To convert hex to binary, replace each hex digit with exactly four bits. If a digit produces fewer than four bits, write the leading zeros; for example, 4 = 0100, not 100.
将十六进制转换为二进制时,把每一位十六进制数字替换为恰好四位二进制位。如果某位产生的比特不足四位,要写出前导 0;例如 4 = 0100,而不是 100。
A4₁₆ = 1010 | 0100 → 10100100₂
This four-bit grouping is the main reason hex is so widely used: a whole byte can be written with just two hex digits, such as FF₁₆ for 11111111₂.
四位分组正是十六进制被广泛使用的主要原因:整个字节只需两个十六进制数字即可写出,例如 FF₁₆ 表示 11111111₂。
9. Converting Between Denary and Hexadecimal | 十进制与十六进制之间的转换
The most direct method from denary to hex is repeated division by 16, recording the remainders and reading them from bottom to top. Any remainder between 10 and 15 is written as A to F.
十进制转换为十六进制最直接的方法是“除 16 取余”,记录余数并从下往上读取。任何在 10 到 15 之间的余数都要写成 A 到 F。
45 ÷ 16 = 2 remainder 13 (D)
2 ÷ 16 = 0 remainder 2
Reading upwards gives 2D₁₆, which matches the binary conversion in the previous section. A longer example: 200₁₀ gives 12 remainder 8, so 200₁₀ = C8₁₆ because 12 is C.
从下往上读取得到 2D₁₆,这与上一节中的二进制转换结果一致。更长的例子:200₁₀ 得到 12 余 8,因此 200₁₀ = C8₁₆,因为 12 写作 C。
For the reverse direction, hex to denary, simply multiply each hex digit by its place value and add. For example, C8₁₆ = 12 × 16 + 8 = 200₁₀.
反向转换时,十六进制转十进制只需将每一位十六进制数字乘以其位权再相加。例如,C8₁₆ = 12 × 16 + 8 = 200₁₀。
10. Why Hexadecimal Is Used in Computing | 为什么在计算机中使用十六进制
Computers work in binary, but humans find long binary strings difficult to read, write and remember. Hexadecimal acts as a human-friendly shorthand because one hex digit represents exactly four bits, making conversion quick and lossless.
计算机内部使用二进制,但人类发现很长的二进制串难以阅读、书写和记忆。十六进制作为对人有好的简写形式,因为一位十六进制数字恰好表示四个比特,转换快速且不会丢失信息。
Real-world examples include memory addresses such as 0x7FFF, where systems software often prints addresses in hex; media access control (MAC) addresses such as 00:1A:2B:3C:4D:5E; IPv6 addresses; colour values in web design such as #FF5733; and error codes displayed by operating systems.
实际应用包括内存地址,例如 0x7FFF,系统软件常以十六进制打印地址;介质访问控制(MAC)地址,例如 00:1A:2B:3C:4D:5E;IPv6 地址;网页设计中的颜色值,例如 #FF5733;以及操作系统显示的十六进制错误代码。
Each of these uses hex because it is compact, easy to convert to and from binary, and far less error-prone than copying a string of thirty-two 0s and 1s. Octal is used less often today, but it still appears in UNIX file permission notation and in some legacy systems.
这些应用都使用十六进制,因为它紧凑、与二进制互转容易,而且比誊写一串三十二个 0 和 1 更不容易出错。八进制如今使用较少,但仍出现在 UNIX 文件权限表示法和一些旧式系统中。
11. Comparison of the Three Number Systems | 三种数制的对比总结
The table below summarises the key features of denary, binary, octal and hexadecimal using the value 45₁₀ throughout. Notice how every representation converges on the same quantity.
下表使用 45₁₀ 来总结十进制、二进制、八进制和十六进制的主要特征。注意每种表示最终都对应同一个数量。
| System | 更多咨询请联系16621398022(同微信)
CommentsMore posts |
|---|
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导