"producing negative zeroes" 在不支持的系统中是什么意思?
What is the meaning of "producing negative zeroes" in a system that doesn't support it?
C17 6.2.6.2/4 说:
If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<,
and >> operators with operands that would produce such a value is undefined.
如果我有一个 2 的补码系统,它不支持负零。它总是利用二进制数的所有可能组合来表示一个值。因此,无论使用哪种按位运算,都不可能产生负零。那么这段文字是什么意思呢?
我的看法是,这部分是指具有 1 的补码或带符号幅度的系统,不支持负零,而是使用填充位或陷阱表示。这是正确的吗?
是的,我认为你的解释是正确的。在二进制补码中,没有可以生成负零的操作,因为这里的概念不存在:设置了符号位的任何值都必然小于 0
.
顺便说一句:奇异的符号表示很可能会从 C2x 中删除,因此所有这些都会消失。
你的解释是正确的。
转到 6.2.6.2 的第 2 段:
For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. There need not be any
padding bits; signed char shall not have any padding bits.
There shall be exactly one sign bit. Each bit that is a
value bit shall have the same value as the same bit in the
object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M ≤ N
). If the sign bit is zero, it shall not affect the resulting
value. If the sign bit is one, the value shall be modified
in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first
two), or with sign bit and all value bits 1 (for ones’
complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero.
这意味着对于给定大小的整数类型,使用补码或符号和大小的实现具有必须为负零或陷阱表示的特定表示。然后由实施来选择其中哪一个适用。
举个例子,假设一个系统有符号和幅度表示以及一个没有填充的 32 位 int
。那么如果支持的话,负零的表示是 0x80000000
.
现在假设执行了以下操作:
int x = 0x7fffffff;
x = ~x;
如果实现支持负零,~
运算符将生成 -0
作为结果并将其存储在 x
中。如果没有,它会创建陷阱表示并根据第 4 段调用未定义的行为。
C17 6.2.6.2/4 说:
If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<, and >> operators with operands that would produce such a value is undefined.
如果我有一个 2 的补码系统,它不支持负零。它总是利用二进制数的所有可能组合来表示一个值。因此,无论使用哪种按位运算,都不可能产生负零。那么这段文字是什么意思呢?
我的看法是,这部分是指具有 1 的补码或带符号幅度的系统,不支持负零,而是使用填充位或陷阱表示。这是正确的吗?
是的,我认为你的解释是正确的。在二进制补码中,没有可以生成负零的操作,因为这里的概念不存在:设置了符号位的任何值都必然小于 0
.
顺便说一句:奇异的符号表示很可能会从 C2x 中删除,因此所有这些都会消失。
你的解释是正确的。
转到 6.2.6.2 的第 2 段:
For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N ). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’ complement), is a trap representation or a normal value. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.
这意味着对于给定大小的整数类型,使用补码或符号和大小的实现具有必须为负零或陷阱表示的特定表示。然后由实施来选择其中哪一个适用。
举个例子,假设一个系统有符号和幅度表示以及一个没有填充的 32 位 int
。那么如果支持的话,负零的表示是 0x80000000
.
现在假设执行了以下操作:
int x = 0x7fffffff;
x = ~x;
如果实现支持负零,~
运算符将生成 -0
作为结果并将其存储在 x
中。如果没有,它会创建陷阱表示并根据第 4 段调用未定义的行为。