C++ 中布尔值的运算符 |=

发布于 2024-12-29 16:07:59 字数 572 浏览 2 评论 0原文

我偶然发现了 C++ 中的以下构造:

bool result = false;
for(int i = 0; i<n; i++){
  result |= TryAndDoSomething(i);
}

我认为此 |= 是 OR 运算符的快捷方式,并且 result 将等于 true最后,如果至少其中一个对 TryAndDoSomething 的调用返回了 true

但现在我想知道是否多个调用实际上可以返回 true。事实上,如果我们将操作扩展为:

result = result || TryAndDoSomething(i);

那么仅当 return 计算为 false 时才会调用该方法,也就是说,如果之前没有其他调用返回 true。因此,在一次调用返回 true 后,将不再执行其他调用。

这是正确的解释吗?

I stumbled upon the following construction in C++:

bool result = false;
for(int i = 0; i<n; i++){
  result |= TryAndDoSomething(i);
}

I supposed that this |= was a shortcut for the OR operator, and that result would equal true in the end if at least one of these calls to TryAndDoSomething had returned true.

But now I am wondering if more than one call can actually return true. Indeed if we extend the operation as:

result = result || TryAndDoSomething(i);

Then the method will be called only if return evaluated to false, that is, if no other call before returned true. Thus after one call returning true, no other call will be done.

Is this the correct interpretation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愁以何悠 2025-01-05 16:07:59

这是按位或赋值,而不是短路或评估。

相当于:

result = result | TryAndDoSomething(i);

不:

result = result || TryAndDoSomething(i);

It's bitwise OR assignment, not short-circuited OR evaluation.

It is equivalent to:

result = result | TryAndDoSomething(i);

Not:

result = result || TryAndDoSomething(i);
任性一次 2025-01-05 16:07:59

对于布尔值,| 产生与 || 相同的结果,但不会短路。 |= 的右操作数始终被求值。

On booleans, | yields the same result as ||, but doesn't short-circuit. The right operand of |= is always evaluated.

落在眉间の轻吻 2025-01-05 16:07:59

在这种情况下,x |= f()(按位或)和 x = x || 之间的唯一区别f()(逻辑或)是后者短路了。在前者中,f() 将被执行 n 次——当然,除非 f() 抛出异常,但那是另一回事了。

||版本中,一旦x变为true,将不再调用f()。 C++ 没有 ||= 运算符,但重要的是要了解 |=||= (如果存在)因此具有不同的语义。 |= 不仅仅是缺失的 ||= 的替代品。

附带说明一下,如果您使用的是 bool,按位运算是安全的,因为标准指定 truefalse< /code> 分别转换为整数 10。因此,这种情况下唯一的区别是急切求值与惰性求值。

The only difference in this context between x |= f() (bitwise OR) and x = x || f() (logical OR) is that the latter is short-circuiting. In the former, f() will be executed n times—unless of course f() throws an exception, but that’s another story.

In the || version, f() will no longer be called once x becomes true. C++ does not have a ||= operator, but it is important to understand that |= and ||= (if it existed) would have different semantics because of this. |= is not just a replacement for the missing ||=.

As a side note, provided you are using bool, the bitwise operation is safe, because the standard specifies that true and false convert to the integers 1 and 0, respectively. So the only difference in this case is eager versus lazy evaluation.

樱花坊 2025-01-05 16:07:59

result |= Try()result = result | 的缩写。尝试();|| 运算符您似乎理解,但 | 运算符却有很大不同。它的名称是按位或(与逻辑或相对)。它具有与对操作数的每个执行a=a||b相同的效果,并且没有逻辑和/或具有的快速救助功能。 (它的速度也疯狂;与加法一样快或更快)。其他按位运算是 &(按位与:每个位上的 a=a&&b)和 ^(按位异或:每个位上的 a=(a!=b))。

result |= Try() is short for result = result | Try();. The || operator you seem to understand, but the | operator is quite different. It's name is bitwise or (as opposed to logical or). It has the same affect as if it performed a=a||b on each bit of the operands, and doesnt have the quick-bailout thing that logical and/or have. (It's also crazy fast; as fast or faster than addition). The other bitwise operations are & (bitwise and: a=a&&b on each bit), and ^ (bitwise xor: a=(a!=b) on each bit).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文