位运算符对 Java 中布尔值的影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1724205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Effect of a Bitwise Operator on a Boolean in Java
提问by Daniel Bingham
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 = 真或 0 = 假。但是布尔值的大小没有定义。它可以大到一个字节,也可以小到一点。
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 是否本质上将其转换为普通的逻辑运算符并继续前进?出于操作的目的,它是否将布尔值视为单个位实体?或者结果未定义以及布尔值的大小?
采纳答案by Noel Ang
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 Specificationfor details.
运营商&
,^
以及|
是当操作数是原始的整数类型位运算符。当操作数为布尔值时,它们是逻辑运算符,并且指定了后一种情况下的行为。有关详细信息,请参阅Java 语言规范的第 15.22.2 节。
回答by LeffeBrune
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
回答by mob
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) isevaluated in the second case,
如果booleanExpression1()
计算结果为false
,那么booleanExpression2()
未在第一种情况下,进行评价booleanExpression2()
(和任何副作用它可以具有)是在第二种情况下评估,
回答by Bernhard Barker
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 coulduse |
and &&
together, but this is not recommended.
如果您希望 OR 具有比 AND 更高的优先级,则可以将|
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
(不需要括号)。