C++标准
If a C++14 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf实现包括在底层字节中填充位unsigned int
,标准是否指定是否不得对填充位执行按位运算?
此外,C++14 标准是否指定是否相等和关系
运算符必须忽略填充位?
指南
如果在这方面缺乏规范,是否有某种形式
对这些运算符对填充位的预期行为达成共识吗?
我在 Stack Overflow 上发现了相互矛盾的答案。轨道上的轻盈竞赛 https://stackoverflow.com/questions/29394518/whats-the-result-of-a-b/29394559#29394559 and ecatmur https://stackoverflow.com/questions/29394518/whats-the-result-of-a-b/29394674#29394674说按位运算符不适合算术,因为它们应用于所有位(包括填充位),而克里斯托夫 https://stackoverflow.com/questions/4475540/c-question-padding-bits-in-unsigned-integers-and-bitwise-operations-c89/4475689#4475689 and 巴泰克·巴纳切维奇 https://stackoverflow.com/questions/29394518/whats-the-result-of-a-b/29394720#29394720假设按位运算符处理整数的逻辑值并忽略填充。
参考
相关答案:关于填充位的存在
(1 https://stackoverflow.com/questions/42297434/does-uint-max-have-all-bits-set-to-1/42297684#42297684,
2 https://stackoverflow.com/questions/3949457/can-an-integer-be-nan-in-c/3949459#3949459,
3 https://stackoverflow.com/questions/12125650/what-do-the-c-and-c-standards-say-about-bit-level-integer-representation-and-m/12125881#12125881),
关于缺乏明确的 C++ 规范
(4 https://stackoverflow.com/questions/29394518/whats-the-result-of-a-b).
填充位的定义C++14 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf- § 3.9.1 - 基本类型:
对于窄字符类型,对象表示的所有位都参与值表示。对于无符号窄字符类型,值表示的所有可能的位模式都表示数字。这些要求不适用于其他类型。
C++14 中对象表示和值表示的定义 - § 3.9 - 类型:
The object representation of an object of type T
is the sequence of N unsigned char
objects taken up by the object of type T
, where N equals sizeof(T)
. The value representation of an object is the set of bits that hold the value of type T
. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.44
脚注 44) 目的是 C++ 的内存模型与 ISO/IEC 9899 编程语言 C 的内存模型兼容。
C++14 中按位 AND 的定义 - § 5.11 - 按位 AND 运算符:
执行通常的算术转换;结果是操作数的按位 AND 函数。该运算符仅适用于整数或无范围枚举操作数。
C++14 中加法的定义 - § 5.7 - 加法运算符:
通常的算术转换是针对算术或枚举类型的操作数执行的。另外,[...]两个操作数都应具有算术或无范围枚举类型[...]。二进制的结果+
运算符是操作数之和。