[英]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 typeint
, withfalse
becoming zero andtrue
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.