常量 32768 和 0x8000 之间的类型差异会产生影响吗?
该标准指定像 0x8000(大于有符号整数的大小)这样的十六进制常量是无符号的(就像八进制常量一样),而像 32768 这样的十进制常量是有符号长整型。 (确切的类型假定为 16 位整数和 32 位长。)但是,在常规 C 环境中,两者将具有相同的表示形式,即二进制 1000 0000 0000 0000
。 这种差异是否真的会产生不同的结果?换句话说,这种差异是否有可能产生重大影响?
The Standard specifies that hexadecimal constants like 0x8000 (larger than fits in a signed integer) are unsigned (just like octal constants), whereas decimal constants like 32768 are signed long. (The exact types assume a 16-bit integer and a 32-bit long.) However, in regular C environments both will have the same representation, in binary 1000 0000 0000 0000
.
Is a situation possible where this difference really produces a different outcome? In other words, is a situation possible where this difference matters at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这可能很重要。如果您的处理器具有 16 位
int
和 32 位long
类型,则 32768 具有long
类型(因为 32767 是最大正数)适合有符号 16 位int
的值),而 0x8000(因为它也被视为unsigned int
)仍然适合 16 位无符号整数
。现在考虑以下程序:
当 32768 被认为是
long
时,求反将反转 32 位,产生
long
类型的表示形式 0xFFFF7FFF;演员阵容是多余。
当 0x8000 被认为是
unsigned int
时,否定将反转16 位,表示为
unsigned int
类型的 0x7FFF;然后,转换将零扩展到
long
值 0x00007FFF。参见 H&S5,第 2.7.1 节,第 24 页。
最好根据需要使用
U
、UL
或L
来扩充常量。Yes, it can matter. If your processor has a 16-bit
int
and a 32-bitlong
type, 32768 has the typelong
(since 32767 is the largest positive value fitting in a signed 16-bitint
), whereas 0x8000 (since it is also considered forunsigned int
) still fits in a 16-bitunsigned int
.Now consider the following program:
When 32768 is considered
long
, the negation will invert 32 bits,resulting in a representation 0xFFFF7FFF with type
long
; the cast issuperfluous.
When 0x8000 is considered
unsigned int
, the negation will invert16 bits, resulting in a representation 0x7FFF with type
unsigned int
;the cast will then zero-extend to a
long
value of 0x00007FFF.Look at H&S5, section 2.7.1 page 24ff.
It is best to augment the constants with
U
,UL
orL
as appropriate.在具有 64 位
long
的 32 位平台上,以下代码中的a
和b
将具有不同的值:On a 32 bit platform with 64 bit
long
,a
andb
in the following code will have different values:另一项尚未给出的检查:将 -1 与 32768 和 0x8000 进行比较(使用大于或小于运算符)。或者,就此而言,尝试将它们与等于 -32768 的“int”变量进行比较。
Another examine not yet given: compare (with greater-than or less-than operators) -1 to both 32768 and to 0x8000. Or, for that matter, try comparing each of them for equality with an 'int' variable equal to -32768.
假设
int
是 16 位,long
是 32 位(这实际上相当不寻常;int
更常见的是 32 位):在大多数情况下在上下文中,数值表达式将隐式转换为由上下文确定的适当类型。 (但这并不总是您想要的类型。)这不适用于可变参数函数的非固定参数,例如格式字符串后面的
*printf()
函数之一的任何参数。Assuming
int
is 16 bits andlong
is 32 bits (which is actually fairly unusual these days;int
is more commonly 32 bits):In most contexts, a numeric expression will be implicitly converted to an appropriate type determined by the context. (That's not always the type you want, though.) This doesn't apply to non-fixed arguments to variadic functions, such as any argument to one of the
*printf()
functions following the format string.区别在于,如果您尝试向 16 位 int 添加一个值,它将无法这样做,因为它将超出变量的范围,而如果您使用 32 位长,您可以添加任何数字小于 2^16 。
The difference would be if you were to try and add a value to the 16 bit int it would not be able to do so because it would exceed the bounds of the variable whereas if you were using a 32bit long you could add any number that is less than 2^16 to it.