C++ 标准对带符号整数类型的最小值和最大值之间关系的保证是什么?
What are C++ Standard guarantees on relation between min and max values of signed integer types?
假设 -LLONG_MAX
(取反 LLONG_MAX
)属于 long long
范围安全吗?
假设如果 LLONG_MIN < -LLONG_MAX
那么 LLONG_MIN == -LLONG_MAX - 1
是否安全?
它是由标准保证的还是所有实际设备都提供 LLONG_MIN == -LLONG_MAX - 1
或 LLONG_MIN == -LLONG_MAX
?
没有。该标准没有规定二进制补码整数表示。
也就是说,绝大多数 C 和 C++ 实现都在 2 的计算机上。
Is it safe to assume that -LLONG_MAX
(negated LLONG_MAX
) belongs to long long
range?
Is it safe to assume that if LLONG_MIN < -LLONG_MAX
then LLONG_MIN == -LLONG_MAX - 1
?
Is it guaranteed by the Standard or just all actual devices provide either LLONG_MIN == -LLONG_MAX - 1
or LLONG_MIN == -LLONG_MAX
?
如果实现使用 2 的补码、1 的补码或符号和大小之一来表示有符号整数类型,那么这三个陈述是正确的。 -LLONG_MAX
在所有三种方案中都在 long long
的范围内,并且 LLONG_MIN
是 -LLONG_MAX
(1 的补码,符号和大小,可能还有 2 的补码)或者是 -LLONG_MAX-1
(可能是 2 的补码)。 2 的补码机器可能会使用该额外值作为陷阱表示,就像 1 的补码、符号和幅度机器可能会使用负零作为陷阱表示一样。因此,如果标准要求实施使用其中一种方案,那么您的问题的答案是 "yes"。
C 标准(C++ 标准在很多地方都遵循)要求 2 的补码、1 的补码或符号和大小:
C11 6.2.6.2 Integer types:
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).
C++ 标准似乎更加开放:
C++14 3.9.1 Fundamental types:
The representations of integral types shall define values by use of a pure binary numeration system51. [Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example]
脚注 51 定义了 "a pure binary numeration system" 的含义,它似乎排除了十进制系统以及偏移系统(其中 0 并非所有位都为零):
51) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position.
实际上,剩下的就是 2 的补码、1 的补码以及符号和大小:C 标准规定的相同方案。我怀疑供应商会接触一台使用新的 21 世纪方案来表示整数并以某种方式满足 C++ 标准法则(但因此不符合 C)的机器。
假设 -LLONG_MAX
(取反 LLONG_MAX
)属于 long long
范围安全吗?
假设如果 LLONG_MIN < -LLONG_MAX
那么 LLONG_MIN == -LLONG_MAX - 1
是否安全?
它是由标准保证的还是所有实际设备都提供 LLONG_MIN == -LLONG_MAX - 1
或 LLONG_MIN == -LLONG_MAX
?
没有。该标准没有规定二进制补码整数表示。
也就是说,绝大多数 C 和 C++ 实现都在 2 的计算机上。
Is it safe to assume that
-LLONG_MAX
(negatedLLONG_MAX
) belongs tolong long
range?Is it safe to assume that if
LLONG_MIN < -LLONG_MAX
thenLLONG_MIN == -LLONG_MAX - 1
?Is it guaranteed by the Standard or just all actual devices provide either
LLONG_MIN == -LLONG_MAX - 1
orLLONG_MIN == -LLONG_MAX
?
如果实现使用 2 的补码、1 的补码或符号和大小之一来表示有符号整数类型,那么这三个陈述是正确的。 -LLONG_MAX
在所有三种方案中都在 long long
的范围内,并且 LLONG_MIN
是 -LLONG_MAX
(1 的补码,符号和大小,可能还有 2 的补码)或者是 -LLONG_MAX-1
(可能是 2 的补码)。 2 的补码机器可能会使用该额外值作为陷阱表示,就像 1 的补码、符号和幅度机器可能会使用负零作为陷阱表示一样。因此,如果标准要求实施使用其中一种方案,那么您的问题的答案是 "yes"。
C 标准(C++ 标准在很多地方都遵循)要求 2 的补码、1 的补码或符号和大小:
C11 6.2.6.2 Integer types:
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).
C++ 标准似乎更加开放:
C++14 3.9.1 Fundamental types:
The representations of integral types shall define values by use of a pure binary numeration system51. [Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example]
脚注 51 定义了 "a pure binary numeration system" 的含义,它似乎排除了十进制系统以及偏移系统(其中 0 并非所有位都为零):
51) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position.
实际上,剩下的就是 2 的补码、1 的补码以及符号和大小:C 标准规定的相同方案。我怀疑供应商会接触一台使用新的 21 世纪方案来表示整数并以某种方式满足 C++ 标准法则(但因此不符合 C)的机器。