Effect of a Bitwise Operator on a Boolean in Java
Asked Answered
C

4

135

The bitwise operators are supposed to travel variables and operate on them bit by bit. In the case of integers, longs, chars this makes sense. These variables can contain the full range of values enforced by their size.

In the case of booleans, however, a boolean can contain only two values. 1 = true or 0 = false. But the size of the boolean isn't defined. It can be as big as a byte or as small a bit.

So what's the effect of using a bitwise operator on a boolean? Does the JVM essentially translate it to a normal logical operator and move on? Does it treat the boolean as a single bit entity for the purpose of the operation? Or is the result undefined along with the size of a boolean?

Conspectus answered 12/11, 2009 at 18:1 Comment(3)
I think you can't use a bitwise operator on a boolean. Only on numbers. I'm sure ~ will not work, I don't know what about other operators.Galantine
You can use some of them, we just discovered an | used in our legacy code. We're removing it, but this code compiled and worked.Conspectus
Since one is short-circuiting and the other isn't (see mobrule's answer), before you change the | to || you may want to make sure the subsequent boolean expressions don't have any side-effects that the original programmer intended to always execute.Numismatist
T
137

The operators &, ^, and | are bitwise operators when the operands are primitive integral types. They are logical operators when the operands are boolean, and their behaviour in the latter case is specified. See the section 15.22.2 of the Java Language Specification for details.

Tamaru answered 12/11, 2009 at 18:11 Comment(5)
Specifically, & and ^ and | are the non-short-circuit logical boolean operators.Donnelldonnelly
If the above is true, why is ideone.com/oGSF7c throwing a null pointer exception? If the |= operator was logical, the program should never have run the x.getValue() directive.Violone
@JohnKrommidas, your x is null, which is why you are getting a NullPointerException. You need to instantiate it.Isthmian
@Ben, that's the whole point. If the operation was bitwise, the VM should never would have bothered to check the second part of the statement. Since it does check it, the operation cannot be bitwise.Violone
@Ben, As @Donnelldonnelly says, the logic is non-short-circuiting, so the second part is evaluated. So a || x.foo() is safe if x is null, but a | x.foo() is not. |= follows the same rules as |.Tophole
J
97

Using the bitwise operator can circumvent short-circuiting behavior:

boolean b = booleanExpression1() && booleanExpression2();
boolean b = booleanExpression1() & booleanExpression2();

If booleanExpression1() evaluates to false, then
booleanExpression2() is not evaluated in the first case, and
booleanExpression2() (and whatever side-effects it may have) is evaluated in the second case,

Jeanne answered 12/11, 2009 at 18:16 Comment(2)
And the bitwise operation performs usually faster than the short-circuit one (provided the evaluation is simple)Enterovirus
The bitwise & will be faster, but the call to the second function could be ignored with the use of &&Jerrine
K
24

Beyond what's covered in the other answers, it's worth noting that && and || have different precedence from & and |.

Extract from the precedence table (with highest precedence at the top).

bitwise AND                 &
bitwise exclusive OR        ^
bitwise inclusive OR        |
logical AND                 &&
logical OR                  ||

What this means to you?

Absolutely nothing, as long as you stick to either only & and | or only && and ||.

But, since | has higher precendence than && (as opposed to ||, which has lower precedence)​, freely mixing them could lead to unexpected behaviour.

So a && b | c && d is the same as a && (b | c) && d,
as opposed to a && b || c && d which would be (a && b) || (c && d).

To prove they're not the same, consider an extract from the truth table:

a | b | c | d | (b|c) | (a&&b) | (c&&d) | a && (b|c) && d | (a&&b) || (c&&d)
F | T | T | T |   T   |   F    |    T   |         F       |        T
                                                  ^                ^
                                                  |- not the same -|

If you want OR to have higher precedence than AND, you could use | and && together, but this is not recommended.

But you really should be putting them in brackets to clarify precedence whenever using different symbols, i.e. (a && b) || c (brackets to clarify precedence), a && b && c (no brackets needed).

Koslo answered 22/8, 2013 at 16:31 Comment(0)
C
4

Even if it will work you shouldn't do it. Language specs define bitwise operators only when both operands are of primitive integer types or both are of boolean type. I'd say for any other case the results are not defined:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5228

Cyrenaic answered 12/11, 2009 at 18:10 Comment(1)
The question is about booleans, not about primitives or a mix of primitives and booleans.Barthol

© 2022 - 2024 — McMap. All rights reserved.