a & b 的结果是什么?

What's the result of a & b?

这很尴尬,但是按位与运算符在 C++ 标准中定义如下(强调我的)。

The usual arithmetic conversions are performed; the result is the bitwise AND function of its operands. The operator applies only to integral or unscoped enumeration operands.

这对我来说似乎毫无意义。据我所知,"bitwise AND function" 在标准中的任何地方都没有定义。

我知道 AND 函数很好理解,因此可能不需要解释。 "bitwise"这个词的意思应该也很清楚:函数作用于其操作数的相应位。但是,操作数的位是由什么构成的还不清楚。

什么给了?

从法律上讲,我们可以认为所有按位运算都具有未定义的行为,因为它们实际上并未定义。

更合理的是,我们期望应用常识并参考这些操作的常见含义,将它们应用于操作数的(因此术语"bitwise").

但实际上并没有说明这一点。可惜我的回答不能算是规范的措辞。

[basic.fundamental]/3 遵循 C 5.2.4.2.1。 C++ 中未指定的按位运算符应该同样遵循 C,在本例中为 6.5.10/4:

,这似乎是合理的

The result of the binary & operator is the bitwise AND of the operands (that is, each bit in the result is set if and only if each of the corresponding bits in the converted operands is set).

注意 C 6.5/4 有:

Some operators (the unary operator ~, and the binary operators <<, >>, &, ^, and |, collectively described as bitwise operators) are required to have operands that have integer type. These operators yield values that depend on the internal representations of integers, and have implementation-defined and undefined aspects for signed types.

整数的内部表示当然在6.2.6.2/1、/2中有描述。

C++ 标准将存储定义为一定数量的位。实现可能会决定赋予特定位什么含义;话虽这么说,二进制 AND 应该对形成特定类型表示的概念 0 和 1 起作用。

3.9.1.7. (...) The representations of integral types shall define values by use of a pure binary numeration system.49 (...)

3.9.1, footnote 49) 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

这意味着对于使用的任何物理表示,二进制 AND 根据 AND 函数的真值 table 起作用(对于每个位编号 i,取位 Ai 和 Bi 来自适当的操作数,并且仅当两者都为 1 时才产生值 1,否则为位 Ri 产生 0。 . 结果值留给实现来解释,但无论选择什么,它都必须符合对其他二元运算(如 OR 和 XOR)的其他期望。

未指定。标准在涉及按位运算时的含义问题是一些缺陷报告的主题。

例如defect report 1857: Additional questions about bits:

The specification of the bitwise operations in 5.11 [expr.bit.and], 5.12 [expr.xor], and 5.13 [expr.or] uses the undefined term “bitwise” in describing the operations, without specifying whether it is the value or object representation that is in view.

Part of the resolution of this might be to define “bit” (which is otherwise currently undefined in C++) as a value of a given power of 2.

响应是:

CWG decided to reformulate the description of the operations themselves to avoid references to bits, splitting off the larger questions of defining “bit” and the like to issue 1943 for further consideration.

defect report 1943说:

CWG decided at the 2014-06 (Rapperswil) meeting to address only a limited subset of the questions raised by issues 1857 and 1861. This issue is a placeholder for the remaining questions, such as defining a “bit” in terms of a value of 2n, specifying whether a bit-field has a sign bit, etc.

从这个defect report 1796: Is all-bits-zero for null characters a meaningful requirement?我们可以看出,这个标准在引用位affected/affects其他部分时的含义的问题也是如此:

According to 2.3 [lex.charset] paragraph 3,

The basic execution character set and the basic execution wide-character set shall each contain all the members of the basic source character set, plus control characters representing alert, backspace, and carriage return, plus a null character (respectively, null wide character), whose representation has all zero bits.

不清楚可移植程序是否可以检查 表示;相反,它似乎仅限于检查 与值表示对应的数字的位 (3.9.1 [basic.fundamental] 第 1 段)。可能更合适 要求空字符值比较等于 0 或 '\0' 而不是指定表示的位模式。

shift, bitwise and, and 的定义也有类似的问题 按位或运算符:那些规范对位的约束 表示的模式或从产生的值 将这些模式解释为数字?

在这种情况下,分辨率将更改为:

representation has all zero bits

至:

value is 0.

请注意, C++ 标准草案确实遵从 3.9.1 部分中的 C 标准部分 5.2.4.2.1 [basic.fundamental] 在段落 3 中它没有引用 C 标准的 6.5/4 部分,这至少会告诉我们结果是实现定义的。我在下面的评论中解释说,C++ 标准只能明确地合并来自规范引用的文本。