Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason for the existence of non-short-circuit logical operators

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?

like image 533
namalfernandolk Avatar asked Feb 13 '12 17:02

namalfernandolk


People also ask

Which operator is a non short circuiting logical?

The | and & logical operators, known as non-short circuit operators, should not be used. Using a non-short circuit operator reduces the efficiency of the program, is potentially confusing and can even lead to the program crashing if the first operand acts as a safety check for the second.

What is the purpose of the NOT logical operator?

The NOT logical operator reverses the true/false outcome of the expression that immediately follows. The NOT operator affects only the expression that immediately follows, unless a more complex logical expression is enclosed in parentheses.

Why logical operators are called short-circuit?

In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.

Are logical operators evaluated with short-circuit?

Do logical operators in the C language are evaluated with the short circuit? Explanation: None.


1 Answers

Updated answer:

Apologies, I missed the word "logical" in your question even though it is there. (I've taken the liberty of emphasizing it a bit with an edit.)

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 } 

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) {     // ... } 

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.

Original answer:

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).

like image 107
T.J. Crowder Avatar answered Sep 28 '22 11:09

T.J. Crowder