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.
但是,对于布尔值,布尔值只能包含两个值。1 = true, 0 = false。但是布尔值的大小没有定义。它可以像字节一样大,也可以小一点。
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?
那么使用位运算符对布尔函数有什么影响呢?JVM是否本质上将它转换为普通的逻辑运算符并继续工作?它是否将布尔值作为操作的一个单位实体?或者结果与布尔值的大小没有定义?
4 个解决方案
#1
94
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.
^操作符&,|时逐位运算符的操作数是原始积分类型。当操作数为布尔值时,它们是逻辑运算符,并指定它们在后一种情况下的行为。有关详细信息,请参阅Java语言规范的15.22.2节。
#2
72
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,
如果booleanExpression1()计算结果为false,则booleanExpression2()在第一个案例中没有被评估,而booleanExpression2()(以及它可能产生的任何副作用)在第二个案例中被评估,
#3
15
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)
.
所以a & b | c & d与a & (b | c) & d是一样的,而a & b || c & d是(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.
如果您希望或希望具有比AND更高的优先级,您可以使用|和&&并在一起使用,但不建议这样做。
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).
但是当你使用不同的符号时,你真的应该把它们放在括号中以澄清优先级,例如(a && b) || c(括号中明确优先级),a && b & c(不需要括号)。
#4
2
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
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html # 5228
#1
94
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.
^操作符&,|时逐位运算符的操作数是原始积分类型。当操作数为布尔值时,它们是逻辑运算符,并指定它们在后一种情况下的行为。有关详细信息,请参阅Java语言规范的15.22.2节。
#2
72
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,
如果booleanExpression1()计算结果为false,则booleanExpression2()在第一个案例中没有被评估,而booleanExpression2()(以及它可能产生的任何副作用)在第二个案例中被评估,
#3
15
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)
.
所以a & b | c & d与a & (b | c) & d是一样的,而a & b || c & d是(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.
如果您希望或希望具有比AND更高的优先级,您可以使用|和&&并在一起使用,但不建议这样做。
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).
但是当你使用不同的符号时,你真的应该把它们放在括号中以澄清优先级,例如(a && b) || c(括号中明确优先级),a && b & c(不需要括号)。
#4
2
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
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html # 5228