开发者

Effect of a Bitwise Operator on a Boolean in Java

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.

开发者_如何学Python

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?


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, then
booleanExpression2() is not evaluated in the first case, and
booleanExpression2() (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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜