说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C4-Binary/exercises.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
4.10. Exercises
- 第 4 章全部练习(英文交互式练习)
附加练习
Additional Exercises
- 值 0b01001010 的十进制和十六进制表示分别是什么?
- What are the decimal and hexadecimal representations for the value 0b01001010?
- 值 389 的二进制和十六进制表示分别是什么?
- What are the binary and hexadecimal representations for the value 389?
- 作为一只有五条腕的生物,海星 Sally 喜欢用五进制数制来表示数。如果 Sally 给你一个五进制数 1423,它等于十进制的多少?
- As a five-armed creature, Sally the starfish prefers to represent numbers using a base 5 number system. If Sally gives you the base 5 number 1423, what is the equivalent decimal value?
答案
Solutions
如果你的浏览器支持隐藏答案,点击这里显示答案。
If your browser supports hidden solutions, click here to reveal them.
\
0b01001010 的十进制值是:
(0 * 27) + (1 * 26) + (0 * 25) + (0 * 24) + (1 * 23) + (0 * 22) + (1 * 21) + (0 * 20)
= 0 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 74
用十六进制表示是:
1
20100 1010
4 A -> 0x4A
0b01001010 in decimal is:
(0 * 27) + (1 * 26) + (0 * 25) + (0 * 24) + (1 * 23) + (0 * 22) + (1 * 21) + (0 * 20)
= 0 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 74
In hexadecimal, it’s:
1
2 0100 1010
4 A -> 0x4A
把 389 转换为十进制……
使用 2 的幂:
389 中放得下 256,所以 d8 应当是 1,还剩 389 - 256 = 133。
133 中放得下 128,所以 d7 应当是 1,还剩 133 - 128 = 5。
5 中放不下 64,所以 d6 应当是 0。
5 中放不下 32,所以 d5 应当是 0。
5 中放不下 16,所以 d4 应当是 0。
5 中放不下 8,所以 d3 应当是 0。
5 中放得下 4,所以 d2 应当是 1,还剩 6 - 5 = 1。
1 中放不下 2,所以 d1 应当是 0。
1 中放得下 1,所以 d0 应当是 1,还剩 1 - 1 = 0。
因此,十进制的 389 对应于 0b110000101。
使用反复除以 2:
389 是奇数,所以 d0 应当是 1。
389 / 2 = 194,是偶数,所以 d1 应当是 0。
194 / 2 = 97,是奇数,所以 d2 应当是 1。
97 / 2 = 48,是偶数,所以 d3 应当是 0。
48 / 2 = 24,是偶数,所以 d4 应当是 0。
24 / 2 = 12,是偶数,所以 d5 应当是 0。
12 / 2 = 6,是偶数,所以 d6 应当是 0。
6 / 2 = 3,是奇数,所以 d7 应当是 1。
3 / 2 = 1,是奇数,所以 d8 应当是 1。
1 / 2 = 0,所以第九位及以上的所有数位都是 0。
因此,十进制的 389 对应于 0b110000101。
转换为十六进制:
1
20001 1000 0101
1 8 5 -> 0x185
Converting 389 to decimal…
Using powers of two:
256 fits into 389, so d8 should be a 1. That leaves 389 - 256 = 133.
128 fits into 133, so d7 should be a 1. That leaves 133 - 128 = 5.
64 does not fit into 5, so d6 should be a 0.
32 does not fit into 5, so d5 should be a 0.
16 does not fit into 5, so d4 should be a 0.
8 does not fit into 5, so d3 should be a 0.
4 fits into 5, so d2 should be a 1. That leaves 6 - 5 = 1.
2 fits does not fit into 1, so d1 should be a 0.
1 fits into 1, so d0 should be a 1. That leaves 1 - 1 = 0.
Thus, decimal 389 corresponds to 0b110000101.
Using repeated division:
389 is odd, so d0 should be a 1.
389 / 2 = 194, which is even, so d1 should be a 0.
194 / 2 = 97, which is odd, so d2 should be a 1.
97 / 2 = 48, which is even, so d3 should be a 0.
48 / 2 = 24, which is even, so d4 should be a 0.
24 / 2 = 12, which is even, so d5 should be a 0.
12 / 2 = 6, which is even, so d6 should be a 0.
6 / 2 = 3, which is odd, so d7 should be a 1.
3 / 2 = 1, which is odd, so d8 should be a 1.
1 / 2 = 0, so any digit numbered nine or above will be 0.
Thus, decimal 389 corresponds to 0b110000101.
Converting to hexadecimal:
1
2 0001 1000 0101
1 8 5 -> 0x185
译注
原答案有两处笔误:开头的「把 389 转换为十进制」应为「转换为二进制」;第七步「还剩 6 - 5 = 1」应为 5 - 4 = 1(译文保留了原样,便于对照)。
五进制的 1423 转换为十进制是:
(1 * 53) + (4 * 52) + (2 * 51) + (3 * 50)
= 125 + 100 + 10 + 3 = 238
1423 in base 5 converted to decimal is:
(1 * 53) + (4 * 52) + (2 * 51) + (3 * 50)
= 125 + 100 + 10 + 3 = 238