c - 这是 C 标准中整数类型符号位定义的错误吗?

标签 c standards language-lawyer

我认为在 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/

相关文章:

c - 为什么读取由其他线程修改的变量既不是旧值也不是新值

c - 声明带有存储类说明符但没有类型说明符的变量是什么意思?

c++ - 使用引用的 constexpr 静态成员作为模板参数

c - POSIX C 线程中上下文切换之前当前堆栈指针寄存器的值存储在哪里

objective-c - 使用 CommonCrypto 生成加盐 key

c++ - 在 cout 中打印 getline() 字符串时出现奇怪的错误

c++ - 约束成员函数和显式模板实例化

c - scanf(),字段宽度,inf 和 nan

python - 在 C 中嵌入 Python - .py 文件中的语法错误第 1 行

c - C 中的整数溢出 : standards and compilers