一个 16 位字只是大小为 16 位的内存量。单词“word”意味着 CPU 可以通过一条指令从内存中读取它。(例如,我在一台有 64K 字节内存的机器上工作,但 CPU 只能以 32K 16 位字的形式访问它。)
解释为无符号整数,16 位字看起来像 0 到 2 16 -1 = 65,535 之间的数字,但不一定是无符号整数。在 的情况下$?
,它用于存储三个无符号整数。
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 15| 14| 13| 12| 11| 10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
\-----------------------------/ \-/ \-------------------------/
Exit code core Signal that killed
(0..255) dumped (0..127)
(0..1)
如果操作系统想要返回“Exited with error code 2”,它会设置 $? 到(2 << 8) | (0 << 7) | (0 << 0)
.
+---+---+---+---+---+---+---+---+
| 2 | << 8
+---+---+---+---+---+---+---+---+
+---+
| 0 | << 7
+---+
+---+---+---+---+---+---+---+
| 0 | << 0
+---+---+---+---+---+---+---+
=================================================================
+---+---+---+---+---+---+---+---+
| 2 |
+---+---+---+---+---+---+---+---+
+---+
| 0 |
+---+
+---+---+---+---+---+---+---+
| 0 |
+---+---+---+---+---+---+---+
=================================================================
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 2 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
如果操作系统想要返回“被信号 5 杀死;核心转储”,它会设置 $? 到(0 << 8) | (1 << 7) | (5 << 0)
.
+---+---+---+---+---+---+---+---+
| 0 | << 8
+---+---+---+---+---+---+---+---+
+---+
| 1 | << 7
+---+
+---+---+---+---+---+---+---+
| 5 | << 0
+---+---+---+---+---+---+---+
=================================================================
+---+---+---+---+---+---+---+---+
| 0 |
+---+---+---+---+---+---+---+---+
+---+
| 1 |
+---+
+---+---+---+---+---+---+---+
| 5 |
+---+---+---+---+---+---+---+
=================================================================
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 5 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
$? >> 8
只是做相反的操作。
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 2 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
>> 8
=================================================================
+---+---+---+---+---+---+---+---+
| 2 |
+---+---+---+---+---+---+---+---+
它返回存储在第 8 位及更高位中的数字。