我认为在 ISO/IEC 9899:TC3 C standard 的第 6.2.6.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; 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 −(2^N) (two’s complement);
- the sign bit has the value −(2^N − 1) (ones’ complement)
在上一节中,N 被定义为有符号类型的值位数,但这里是无符号 类型的值位数。
以每字节 8 位和二进制补码的 signed char
为例,这表示符号位的值为 -(2^8) =-256 而不是 -(2^7 ) = -128。
我认为标准应该在开头的段落中切换 M 和 N,或者更改符号位的定义以使用 M:
- the sign bit has the value −(2^M) (two’s complement);
- the sign bit has the value −(2^M − 1) (ones’ complement)
我是否遗漏了什么,或者这是一个错误?
最佳答案
在C11 draft standard (Jonathan Leffler 确认最终标准也包含此措辞)它确实从使用 N
切换到使用 M
:
- the sign bit has the value −(2M) (two’s complement);
- the sign bit has the value −(2M − 1) (ones’ complement).
我找不到缺陷报告,但这取决于:
If there are N value bits
段落 1
也适用于段落 2
这不是一个不合理的解释,它只是高度模棱两可:
(if there are M value bits in the signed type and N in the unsigned type, then M <= N)
关于c - 这是 C 标准中整数类型符号位定义的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223909/