问题描述
比方说,我有一个变量 i
来自外部资源:
Let's say I have a variable i
that comes from external sources:
int i = get_i();
假设 i
为 INT_MIN
和二进制补码表示法, -i
是否未定义?
Assuming i
is INT_MIN
and two's complement representation, is -i
undefined?
推荐答案
这取决于平台。 C支持负数的三种表示形式(请参见节)。如果是陷阱表示,则 -INT_MIN
等于 INT_MAX
。
With one's complement and sign and magnitude, -INT_MIN
is defined (and equal to INT_MAX
). With two's complement, it depends on whether the value with sign bit 1 and all value bits zero is a trap representation or a normal value. If it's a normal value, -INT_MIN
overflows, resulting in undefined behavior (see section 6.5 of the C99 standard). If it's a trap representation, -INT_MIN
equals INT_MAX
.
也就是说,大多数现代平台使用的二进制补码没有陷阱表示,因此 -INT_MIN
通常会导致不确定的行为。
That said, most modern platforms use two's complement without trap representations, so -INT_MIN
typically results in undefined behavior.
这篇关于否定INT_MIN是不确定的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!