“| =”是什么意思? (管道等运算符)

时间:2013-01-12 16:43:20

标签: java android operators

我尝试使用Google搜索和Stack Overflow进行搜索,但它没有显示任何结果。我在开源库代码中看到了这个:

Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;

“| =”(pipe equal operator)是什么意思?

6 个答案:

答案 0 :(得分:252)

|=的读取方式与+=相同。

notification.defaults |= Notification.DEFAULT_SOUND;

相同
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;

其中|是按位OR运算符。

所有运营商都被引用here

使用逐位运算符,因为这些常量使得int能够携带标志。

如果你look处于这些常数,你会发现他们有两个权力:

public static final int DEFAULT_SOUND = 1;
public static final int DEFAULT_VIBRATE = 2; // is the same than 1<<1 or 10 in binary
public static final int DEFAULT_LIGHTS = 4; // is the same than 1<<2 or 100 in binary

所以你可以使用逐位OR来添加标志

int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // same as 001 | 010, producing 011

所以

myFlags |= DEFAULT_LIGHTS;

只是意味着我们添加一个标志。

对称地,我们使用&测试标志:

boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;

答案 1 :(得分:33)

您的问题已经得到了足够的答案。但可能是我的回答可以帮助您更多地了解|=种二元运算符。

我正在为bitwise operators撰写表格:
以下是有效的:

----------------------------------------------------------------------------------------
Operator   Description                                   Example
----------------------------------------------------------------------------------------
|=        bitwise inclusive OR and assignment operator   C |= 2 is same as C = C | 2
^=        bitwise exclusive OR and assignment operator   C ^= 2 is same as C = C ^ 2
&=        Bitwise AND assignment operator                C &= 2 is same as C = C & 2
<<=       Left shift AND assignment operator             C <<= 2 is same as C = C << 2
>>=       Right shift AND assignment operator            C >>= 2 is same as C = C >> 2  
----------------------------------------------------------------------------------------

注意所有运算符都是二元运算符。

同样注意: (以下几点我想添加我的答案)

  • >>>是Java中的按位运算符,称为无符号转移
    >>>=不是Java中的运算符。 >>>= operator

  • ~是按位补码位,0 to 1 and 1 to 0(一元运算符),但~=不是运算符。

  • 此外,!调用逻辑非运算符,但!=检查两个操作数的值是否相等,如果值不相等则条件变为真。例如(A != B) is true。其中A=!B表示如果Btrue,则A成为false(如果Bfalse则为{{1}成为A)。

旁注: true不称为管道,而是称为OR,管道是shell术语,将一个流程转移到下一个..

答案 2 :(得分:13)

我一直在寻找关于|=在Groovy中做什么的答案,虽然上面的答案是正确的,但它们并没有帮助我理解我正在查看的特定代码。

特别是,当应用于布尔变量&#34; | =&#34;将在第一次遇到右侧的truthy表达式时将其设置为TRUE,并为所有| =后续调用保持其TRUE值。就像一个闩锁。

这是一个简单的例子:

groovy> boolean result  
groovy> //------------ 
groovy> println result           //<-- False by default
groovy> println result |= false 
groovy> println result |= true   //<-- set to True and latched on to it
groovy> println result |= false 

输出:

false
false
true
true

修改的: 为什么这有用?

考虑一种情况,您想知道各种对象上是否有任何更改,如果是,请通知其中一项更改。因此,您需要设置hasChanges布尔值,并将其设置为|= diff (a,b),然后设置为|= dif(b,c)等。 这是一个简短的例子:

groovy> boolean hasChanges, a, b, c, d 
groovy> diff = {x,y -> x!=y}  
groovy> hasChanges |= diff(a,b) 
groovy> hasChanges |= diff(b,c) 
groovy> hasChanges |= diff(true,false) 
groovy> hasChanges |= diff(c,d) 
groovy> hasChanges 

Result: true

答案 3 :(得分:12)

这是对此的缩短:

notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;

并且|是一个有点的OR。

答案 4 :(得分:8)

|bitwise-or运算符,它的应用类似于+=

答案 5 :(得分:2)

注意:|| =不存在。 (逻辑或) 你可以使用

img{max-width:100%; width:auto; vertical-align: top;}

y= y || expr; // expr is NOT evaluated if y==true