简体   繁体   English

在布尔值上使用按位非运算符(〜)是否会调用未定义的行为?

[英]Does using bitwise not operator (~) on boolean values invoke Undefined Behavior?

If a C++ program applies the bitwise-not operator (~) to a boolean value, does that invoke Undefined Behavior? 如果C ++程序将bitwise-not运算符(〜)应用于布尔值,那么它是否会调用未定义的行为?

Eg is the following program well-defined? 例如,以下程序定义明确吗?

bool f = false;
bool f2 = ~f;    // is f2 guaranteed to be true, or is this UB?
bool t = true;
bool t2 = ~t;    // is t2 guaranteed to be false, or is this UB?

(Yes, I know there is a ! operator that is better-suited to this sort of thing; for purposes of this question we will ignore its existence ;)) (是的,我知道有一个更适合这类事情的!运算符;出于这个问题的目的,我们将忽略它的存在;))

5.3.1/10 The operand of ~ shall have integral or unscoped enumeration type; 5.3.1 / 10 ~的操作数应具有整数或无范围的枚举类型; the result is the one's complement of its operand. 结果是其操作数的一个补码。 Integral promotions are performed. 执行整体促销。 [emphasis mine] [强调我的]

4.5/6 A prvalue of type bool can be converted to a prvalue of type int , with false becoming zero and true becoming one. 4.5 / 6 bool类型的prvalue可以转换为int类型的prvalue, false变为零, true变为1。

4.5/7 These conversions are called integral promotions . 4.5 / 7这些转换称为整体促销

So ~false is an int with a bit pattern consisting of all ones - one's complement of a bit pattern representing 0, namely all zeros (as required by 3.9.1/7 .) Similarly, ~true is an int that's one's complement of the bit representation of 1 - namely, all ones with the least significant bit zero. 所以~false是一个带有由所有1组成的位模式的int - 表示0的位模式的一个补码,即全零(按3.9.1 / 7的要求)。同样, ~true是一个int ,它是一个补码1的位表示 - 即,具有最低有效位0的所有1。 Both these values will evaluate to true in boolean context. 这两个值在布尔上下文中都将计算为true

The arithmetic operators perform the integral promotions on their operand. 算术运算符在其操作数上执行整数提升。 Specifically [expr.unary.op]/9 says that this happens for ~ too. 具体地说[expr.unary.op] / 9说,这种情况为~太。

So ~t is the same as ~1 . 所以~t~1相同。 This gives a valid non-zero integer. 这给出了一个有效的非零整数。

Integer-to-bool conversions are defined by [conv.bool]: 整数到布尔的转换由[conv.bool]定义:

A zero value, null pointer value, or null member pointer value is converted to false; 零值,空指针值或空成员指针值转换为false; any other value is converted to true 任何其他值都转换为true

so bool t2 = ~t; 所以bool t2 = ~t; yields t2 == true . 产量t2 == true There is no undefined behaviour. 没有未定义的行为。


~f is the same as ~0 . ~f~0相同。 In 2's complement, ~0 gives -1 , so we will have f2 == true . 在2的补码中, ~0给出-1 ,所以我们将得到f2 == true

In 1's complement -- if there were ever a C++ system that uses 1's complement -- then the effect of ~0 is unclear. 在1的补数-如果有以往任何时候都使用1的补C ++系统-那么的效果~0还不清楚。

bool t = true;
bool t2 = ~t;    // is t2 guaranteed to be false, or is this UB?

I suppose it is not guaranteed, seeing how 我想这不保证,看看怎么样

  bool b = true;
  bool b1 = ~b;
  cout << b1;

outputs a "true" 输出“真实”

I suppose it has to do with boolean representation... if it is a byte, then 00000001 will negate to 11111110 which is not zero. 我想它与布尔表示有关...如果它是一个字节,那么00000001将否定为11111110 ,这不是零。 Promotion might also be at play, but it's the same tune. 促销也可能起作用,但它是相同的曲调。

The key here is it is "bitwise" not "logical". 这里的关键是“按位”而非“逻辑”。 So one should not expect the two to match, unless the boolean representation is a single bit. 因此,除非布尔表示是单个位,否则不应期望两者匹配。

Easily entirely defined behavior. 轻松完全定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用联合的这种类型别名会调用未定义的行为吗? - Does this type aliasing using union invoke undefined behavior? 标准引用:超出范围调用`vector :: operator []`是否会调用未定义的行为? - Standard citation: Does an out of range call to `vector::operator[]` invoke Undefined Behavior? 在这种情况下,三元运算符与 C 风格的指针向上转换相结合会调用未定义的行为吗? - Does the ternary operator combined with C-style upcasting of pointers invoke undefined behavior in this instance? 以下代码是否调用未定义的行为? - Does the following code invoke Undefined Behavior? 使用按位和布尔 - Using bitwise & on boolean 这个c ++ 11 lambda代码是否会调用未定义的行为? - Does this c++11 lambda code invoke undefined behavior? 将对象的实例作为函数参数传递 - 此代码是否调用未定义的行为? - Passing an instance of object as function argument — Does this code invoke undefined behavior? 使用reinterpret_cast是否会引起未定义的行为? - Does this use of reinterpret_cast invoke an undefined behavior? 条件运算符中的未定义行为 - undefined behavior in conditional operator 使用没有显式强制转换的常量调用未定义行为? - is using a constant without an explicit cast invoke undefined behavior?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM