r/javahelp • • Jul 13 '19

Are non short-circuit logical operators ( | & ) refered to as bitwise operators?

That's one thing that confuses me.

From my research online, it seems that | and & are referred to as "bitwise operators", even if they are used as logical operators between 2 boolean expressions. What do they have to do with bitwise operations, besides using the same operator symbols?

Here an example:

The non-bitwise operators && and || are short-circuit operators. In other words, with &&, if the LHS is false, the RHS will never be evaluated; with || if the LHS is true, then the RHS will never be evaluated. On the other hand, the bitwise operators & and | are non-short-circuit, and will always evaluate both the LHS and the RHS.

https://softwareengineering.stackexchange.com/questions/68183/when-is-it-appropriate-to-use-a-bitwise-operator-in-a-conditional-expression

18 Upvotes

18 comments sorted by

View all comments

0

u/[deleted] Jul 13 '19

[deleted]

9

u/marvk Jul 13 '19

It's the same as if you were to evaluate if(1 - 1) or if(123 * 23 + 5)

These are syntax errors in Java.

Edit: to be clear, & and | are not boolean or conditional operators and should not be used as such.

This is false. See my answer from below:

& and | are operators that can be used on numbers and on booleans. If you use them on booleans, they will not short circuit.

Suppose you have a method with a side effect:

boolean m() {
    System.out.println("I'm a side effect");
    return true;
}

In the following example, the side effect will not be visible:

if (false && m()) {
    ....
}

while in this snippet will print I'm a side effect to the console:

if (false & m()) {
    ....
}

On operands without side effects, they will produce the same result.

Try it online

Also see this SO thread for further explanation.

1

u/[deleted] Jul 13 '19 edited Jul 13 '19

These are syntax errors in Java.

Huh really? TIL...

Anyway, I insist that you shouldn't use bitwise operators as conditionals (unless of course they're part of a bitwise operation that is evaluated). Even if it works I think it's unnecessarily confusing and bad style. I just don't see why, if you want to perform some operation regardless of the outcome you shouldn't make it part of a conditional check imo. It's just needlessly obtuse.

But I guess that's a style preference, I just haven't ever seen it used like that, nor would I personally.

To elaborate, the purpose of a conditional check is to evaluate whether something should be done or not. How this check is evaluated may be subject to change. That shouldn't change what the program does imo. The operations should be inside the following code block. That's how you expect the structure of the program to be, it's too easy to miss stuff if it's hidden in conditionals. Adding an extra | or & shouldn't break functionality if the expressions they evaluate stay the same.

1

u/marvk Jul 13 '19

I agree that it's a bad idea to rely on them. However, you really can't call them bitwise operators if they're used with booleans.

1

u/Fr4nkWh1te Jul 13 '19

What confuses me is that many people refer to them as "bitwise operators", even if they are used on booleans.

5

u/thorndark Jul 13 '19

Think about what a boolean actually is and what these operations actually are. A boolean is just a value where we've hard coded 0 to mean false and 1 to mean true. Bitwise AND is an operation where, for each bit position in the input, it compares the the two corresponding bits and sets the bit in the result to be 0 if either of them is 0 or 1 otherwise.

Maybe another way to think about it is that && operates on true/false. The bitwise operators operate on whatever bits are there; they ignore whatever higher level concepts those bits might represent (and so they can't short circuit because they have to work in the case where you're looking at more than just one bit).

2

u/Fr4nkWh1te Jul 14 '19

So in summary, & and | are called "bitwise operators", even when they are used between 2 booleans?

0

u/mobrockers Jul 14 '19

No, they're not ever used between two booleans. As soon as you place the bitwise operator you're working with bits not bools.

2

u/Fr4nkWh1te Jul 14 '19

But in my Java code they are between 2 variables of type boolean

1

u/mobrockers Jul 14 '19

Yes but the comparison is done on the bit value of the bool.

1

u/marvk Jul 14 '19

OP, what that guy said is confusing at best. Maybe this helps.

About calling them bitwise operators, the Java Specs seem to only call them bitwise operators, but then again, they don't specifically mention a string concatenation operator (+).

I personally would not call them bitwise operators when using them as logical operators.

1

u/marvk Jul 14 '19 edited Jul 14 '19

Stop confusing OP and stop spreading false information.

No, they're not ever used between two booleans.

This is wrong, please read my comment.

As soon as you place the bitwise operator you're working with bits not bools.

There's no need to think about the underlying implementation, which you don't even know, because it depends on the JVM you are using.

Booleans can not be cast to numbers (bits) in Java.

For all intents and purposes, |, & and ^ are overloaded operators that can be used on numbers and booleans, and booleans don't turn into a number when you use the operators on them. They can be either a bitwise operator on numbers, or non-short-circuting logical operators.

1

u/mobrockers Jul 14 '19

When both operands of a &, , or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2

Fair enough you shouldn't need to care about the implementation details. But the operators are called bitwise operators. That's just their name.

1

u/marvk Jul 14 '19

Yeah, as I said somewhere in this thread, I know that in the specs they're generally called bitwise operators. However, even the section title reads

15.22.2. Boolean Logical Operators &, , and |

3

u/marvk Jul 13 '19

They are different operations. Just like + can be used for String concatenation and addition, | and & can be used as bitwise operations or as non-short-circuting logical operators.