Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数的表示:进制及进制转换 #153

Open
dushaoshuai opened this issue Oct 27, 2024 · 0 comments
Open

数的表示:进制及进制转换 #153

dushaoshuai opened this issue Oct 27, 2024 · 0 comments
Labels
C The C programming language. draft Still only a draft Go The Go programming language. Operating system Operating system 中级软件设计师 计算机基础

Comments

@dushaoshuai
Copy link
Owner

dushaoshuai commented Oct 27, 2024

8 和 16 是 2 的幂,比起 10 进制,8 进制和 16 进制更接近计算机数据的表示方式。

通常我们采用 10 进制计数。10 进制数 2157 可以表示为:

$$2 \times 1000 + 1 \times 100 + 5 \times 10 + 7 \times 1$$

也就是:

$$2 \times 10^3 + 1 \times 10^2 + 5 \times 10^1 + 7 \times 10^0$$

10 进制使用 10 的幂计数。同理,2 进制使用 2 的幂计数,比如,机器数 $1101_{2}$ 可以表示为:

$$1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1\times 2^0$$

这是 2 进制换算为 10 进制的方法,数 $1101_{2}$ 的真值是:

$$1 \times 8 + 1 \times 4 + 0 \times 2 + 1 \times 1 = 13$$

也就是 $1101_{2} = 13_{10}$.

N 进制使用 0 ~ N-1 的 N 个符号(大于 9 的数字依次用 A, B, C, D... 表示)表示数据,使用 N 的幂计数,数 $abcdef_{N}(a, b, c, d, e, f \leq N - 1)$ 可以表示为:

$$a \times N^5 + b \times N^4 + c \times N^3 + d \times N^2 + e \times N^1 + f \times N^0$$

这是 N 进制数换算为 10 进制的方法。

N 进制转换为 10 进制

$abcdef_{N}(a, b, c, d, e, f \leq N - 1)$ 换算为 10 进制是:

$$a \times N^5 + b \times N^4 + c \times N^3 + d \times N^2 + e \times N^1 + f \times N^0$$

10 进制转换为 N 进制

  1. 除以目标进制 N,记录余数,这个余数就是 N 进制下的最低位。
  2. 将商继续除以 𝑁,再记录余数,依次类推,直到商为 0。
  3. 最终的余数,从最后一个得到的余数到第一个得到的余数,按照逆序排列,就得到该十进制数的 N 进制表示。

例如 $19765_{10}$ 换算为 16 进制的过程是:

  1. $19765_{10}$ 连续除以 2,记录余数,直到商为 0 为止:

$$\begin{array}{c|c} \text{商} & \text{余数} \\ \hline 19765 \div 16 = 1235 & \text{余数 } 5 \\ 1235 \div 16 = 77 & \text{余数 } 3 \\ 77 \div 16 = 4 & \text{余数 } 13 \\ 4 \div 16 = 0 & \text{余数 } 4 \\ \end{array}$$

  1. 将余数从下往上排列,得到二进制表示:
$$19765_{10} = 4D35_{2}$$

2 进制和 8 进制相互转换

一个 8 进制数字对应 3 个二进制数字:

8 进制 2 进制
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

要将数 $355_{8}$ 转换为 2 进制,将每一个 8 进制数字转换为对应的 2 进制表示,按照顺序连接起来:

  • 3 -> 011
  • 5 -> 101
  • 5 -> 101
  • 755 -> 011101101

因此,$755_{8} = 11101101_{2}$

要将 $1110101110_{2}$ 转换为 8 进制,从最低位开始,将 2 进制数字划分为 3 个一组,按照顺序连接起来:

  • 1 -> 1
  • 110 -> 6
  • 101 -> 5
  • 110 -> 6
  • 1110101110 -> 1656

因此,$1110101110_{2} = 1656_{8}$

2 进制和 16 进制相互转换

一个 16 进制数字对应 4 个二进制数字:

16 进制 2 进制
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

要将数 $3A5C_{16}$ 转换为 2 进制,将每一个 16 进制数字转换为对应的 2 进制表示,按照顺序连接起来:

  • 3 -> 0011
  • A -> 1010
  • 5 -> 0101
  • C -> 1100
  • 3A5C -> 11101001011100

因此,$3A5C_{16} = 11101001011100_{2}$

要将 $1110101110_{2}$ 转换为 16 进制,从最低位开始,将 2 进制数字划分为 4 个一组,按照顺序连接起来:

  • 11 -> 3
  • 1010 -> A
  • 1110 -> E
  • 1110101110 -> 3AE

因此,$1110101110_{2} = 3AE_{16}$

@dushaoshuai dushaoshuai added C The C programming language. draft Still only a draft Go The Go programming language. Operating system Operating system 中级软件设计师 计算机基础 labels Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C The C programming language. draft Still only a draft Go The Go programming language. Operating system Operating system 中级软件设计师 计算机基础
Projects
None yet
Development

No branches or pull requests

1 participant