问题描述
有人可以清楚地解释一下K& R中的这些行的实际含义:
Can someone explain clearly what these lines from K&R actually mean:
推荐答案
您需要先了解几件事.
-
如果我采用8位值并将其扩展为16位值,通常您会想像只是在左边加上一堆0.例如,如果我的8位值23(二进制)为00010111,则作为16位数字为0000000000010111,也为23.
If I take an 8-bit value and extend it to a 16-bit value, normally you would imagine just adding a bunch of 0's on the left. For example, if I have the 8-bit value 23, in binary that's 00010111, so as a 16-bit number it's 0000000000010111, which is also 23.
事实证明,负数始终在高位为1. (可能有些奇怪的机器不适用,但对于您可能使用的任何机器都是如此.)例如,8位值-40以二进制表示为11011000.
It turns out that negative numbers always have a 1 in the high-order bit. (There might be weird machines for which this is not true, but it's true for any machine you're likely to use.) For example, the 8-bit value -40 is represented in binary as 11011000.
因此,当您将带符号的8位值转换为16位值时,如果高阶位为1(即,如果数字为负),则不要添加一堆0 -s在左侧,而是添加一堆1.例如,回到-40,我们会将11011000转换为1111111111011000,这是-40的16位表示形式.
So when you convert a signed 8-bit value to a 16-bit value, if the high-order bit is 1 (that is, if the number is negative), you do not add a bunch of 0-s on the left, you add a bunch of 1's instead. For example, going back to -40, we would convert 11011000 to 1111111111011000, which is the 16-bit representation of -40.
还有无符号数字,它们永远不会为负.例如,8位无符号数字216表示为11011000.(您会注意到,这与有符号数字-40的位模式相同.)将无符号8位数字转换为16位时,您需要添加一堆0无论如何.例如,您可以将11011000转换为0000000011011000,即216的16位表示形式.
There are also unsigned numbers, that are never negative. For example, the 8-bit unsigned number 216 is represented as 11011000. (You will notice that this is the same bit pattern as the signed number -40 had.) When you convert an unsigned 8-bit number to 16 bits, you add a bunch of 0's no matter what. For example, you would convert 11011000 to 0000000011011000, which is the 16-bit representation of 216.
因此,将所有内容组合在一起,如果要将8位数字转换为16(或更多)位,则必须注意两点.首先,该数字是带符号的还是未签名的?如果它是无符号的,只需在左侧添加一堆0.但是,如果已签名,则必须查看8-0位数字的高位.如果为0(如果数字为正),则在左侧添加一堆0.但是如果它是1(如果数字为负),则在右边添加一堆1. (整个过程称为符号扩展.)
So, putting this all together, if you're converting an 8-bit number to 16 (or more) bits, you have to look at two things. First, is the number signed or unsigned? If it's unsigned, just add a bunch of 0's on the left. But if it's signed, you have to look at the high-order bit of the 8-0bit number. If it's 0 (if the number is positive), add a bunch of 0's on the left. But if it's 1 (if the number is negative), add a bunch of 1's on the right. (This whole process is known as sign extension.)
普通ASCII字符(如"A","1"和"$")的值均小于128,这意味着它们的高位始终为0. "Latin-1"或UTF-8字符集的值大于128.因此,有时它们也被称为高位"或第八位"字符.例如,拉丁1字符Ø(带有斜线的O)的值为216.
The ordinary ASCII characters (like 'A' and '1' and '$') all have values less than 128, which means that their high-order bit is always 0. But "special" characters from the "Latin-1" or UTF-8 character sets have values greater than 128. For this reason they're sometimes also called "high bit" or "eighth bit" characters. For example, the Latin-1 character Ø (O with a slash through it it) has the value 216.
最后,尽管C中的类型char
通常是8位类型,但是 C标准未指定它是带符号还是无符号.
Finally, although type char
in C is typically an 8-bit type, the C Standard does not specify whether it is signed or unsigned.
将所有内容放在一起,Kernighan和Ritchie所说的是,当我们将char
转换为16位或32位整数时,我们不太了解如何应用步骤5.在类型为char
的机器上是无符号的,并且我将字符Ø转换为整数,我可能会得到值216.但是,如果我在类型为char
的机器上,我会可能会得到-40.
Putting this all together, what Kernighan and Ritchie are saying is that when we convert a char
to a 16- or 32-bit integer, we don't quite know how to apply step 5. If I'm on a machine where type char
is unsigned, and I take the character Ø and convert it to an int, I'll probably get the value 216. But if I'm on a machine where type char
is signed, I'll probably get the number -40.
这篇关于字符转换为整数的微妙之处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!