Java的操作员:| =按位OR和分配例子 [英] Java Operators : |= bitwise OR and assign example
问题描述
我刚刚经历code有人写,我看到 | =
的使用,Java的运营商仰视,它表明按位或并指定操作,任何人都可以解释给我它的一个例子吗?
下面是code,上面写着的:
为(字符串搜索:textSearch.getValue())
比赛| = field.contains(搜索);
A | = B;
是相同的
A =(A | B);
它计算的按位或两个操作数,而受让人的结果给左操作数。
要解释你的榜样code:
为(字符串搜索:textSearch.getValue())
比赛| = field.contains(搜索);
我presume 匹配
是布尔
;这意味着,按位运算符行为相同,逻辑运算符。
在循环的每次迭代中,它或
s ^ 匹配的当前值
与任何从返回 field.contains()
。这有其设置为真正
如果是已经成为事实,或如果 field.contains()效果code>返回true。
所以,如果计算出的任何的调用 field.contains()
,在整个循环,又回到了<$ C $的C>真正。
I just going through code someone has written and I saw |=
usage, looking up on Java operators, it suggests bitwise or and assign operation, can anyone explain and give me an example of it?
Here is the code that read it:
for (String search : textSearch.getValue())
matches |= field.contains(search);
a |= b;
is the same as
a = (a | b);
It calculates the bitwise OR of the two operands, and assigns the result to the left operand.
To explain your example code:
for (String search : textSearch.getValue())
matches |= field.contains(search);
I presume matches
is a boolean
; this means that the bitwise operators behave the same as logical operators.
On each iteration of the loop, it OR
s the current value of matches
with whatever is returned from field.contains()
. This has the effect of setting it to true
if it was already true, or if field.contains()
returns true.
So, it calculates if any of the calls to field.contains()
, throughout the entire loop, has returned true
.
这篇关于Java的操作员:| =按位OR和分配例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!