Using bitwise operations for bool helps save unnecessary branch prediction logic by the processor, resulting from a 'cmp' instruction brought in by logical operations.
First, logical operators work on boolean expressions and return boolean values (either true or false), whereas bitwise operators work on binary digits of integer values (long, int, short, char, and byte) and return an integer.
In Java, bitwise operators perform operations on integer data at the individual bit-level. Here, the integer data includes byte , short , int , and long types of data. There are 7 operators to perform bit-level operations in Java.
Bitwise operators are used in expressions with integer values and apply an operation separately to each bit in an integer. The term logical expression refers to an expression in which all of the operands can be reduced to boolean primitives. Logical operators produce a boolean primitive result.
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.
Using the bitwise operator can circumvent short-circuiting behavior:
boolean b = booleanExpression1() && booleanExpression2();
boolean b = booleanExpression1() & booleanExpression2();
If booleanExpression1()
evaluates to false
, thenbooleanExpression2()
is not evaluated in the first case, andbooleanExpression2()
(and whatever side-effects it may have) is evaluated in the second case,
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With