存在非短路逻辑运算符的原因 [英] Reason for the existence of non-short-circuit logical operators

查看:150
本文介绍了存在非短路逻辑运算符的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

布尔操作数一起使用时,& | 逻辑运算符rel =nofollow noreferrer> JLS的第15.22.2节。然而,与&& || 不同,这些不会短路;他们总是评估双方。我有一个愚蠢的问题:为什么效率较低的非短路逻辑运算符(& | )当我们拥有效率更高的短路逻辑运算符时(&& || )?我的意思是,与短路逻辑运算符相比,非短路逻辑运算符的实际用途是什么?换句话说,总是通过使用非短路逻辑运算符来评估双方的用法是什么?

When used with boolean operands, & and | become logical operators per Section 15.22.2 of the JLS. Unlike && and ||, however, these don't short-circuit; they always evaluate both sides. I have a silly question: Why are the less-efficient non-short-circuit logical operators (&, |) still there, when we have the more-efficient short-circuit logical operators (&&, ||)? I mean, what is the real usage of the non-short-circuit logical operators, as opposed to with the short-circuit logical operators? In other words, what is the usage of always evaluating both sides by using the non-short-circuit logical operators?

推荐答案

更新的答案

道歉,我在你的问题中错过了逻辑这个词,即使那里。 (我冒昧地通过编辑强调它。)

考虑你想要任何副作用的情况无论左侧表达式是否计算 true false ,都会发生。例如,对比:

Consider the case where you want any side-effects to always occur, regardless of whether the left-hand expression evaluates true or false. E.g., contrast:

if (foo() & bar()) {
    // Only call this if both operations returned true
}

with

if (foo() && bar()) {
    // Only call this if both operations returned true
}

我们假设 foo 无论 foo 是否返回 true 都会产生想要的效果c $ c>或 false 。在上面的第一个中,我知道 bar 将始终被调用并产生效果。当然,在后者中, bar 可能会被调用,也可能不被调用。如果我们没有非短路版本,我们必须使用临时变量:

Let's assume both foo and bar have effects that we want to have happen regardless of whether foo returns true or false. In the first one above, I know that bar will always get called and have its effect. In the latter, of course, bar may or may not get called. If we didn't have the non-short-circuit version, we'd have to use temporary variables:

boolean fooResult, barResult;
fooResult = foo();
barResult = bar();
if (fooResult && barResult) {
    // ...
}

你可能会争辩(我可能会)你应该无论如何,因为它太容易误读 if(foo()& bar ()),但我们去了,这是一个非短路版本的实用理由。

You might argue (I probably would) that you should do that anyway, because it's way too easy to misread if (foo() & bar()), but there we go, a pragmatic reason for having non-short-circuit versions.

原始答案

您如何建议& (或 | )是一个短路的运营商?使用&& || ,这是有道理的,因为你正在处理布尔条件:它们可以是无论是真是假,都没有灰色阴影。但是& | 处理比特而不是布尔值。结果是一个数字。我的意思是,如果左侧是 0 ,我猜& 无法评估右侧,并且类似地, | 无法评估它,如果左侧是全部位 - 无论类型是什么,但我没有看到制作一个每个运算符的边缘情况显着(与254或更多其他情况相比)。

How would you propose & (or |) be a short-circuited operator? With && and ||, it makes sense because you're dealing with boolean conditions: They can be true or false, there are no shades of grey. But & and | deal with bits, not booleans. The result is a number. I mean, I guess & could not evaluate the right-hand side if the left-hand side were 0, and similarly | could not evaluate it if the left-hand side were all-bits-on for whatever the type was, but I don't see much point to making the one edge case of each operator significant (as compared to the 254 or more other cases).

这篇关于存在非短路逻辑运算符的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆