"|="的意思吗?(管道等算符)

我尝试使用谷歌Search和Stack Overflow进行搜索,但没有显示任何结果。我在开源库代码中看到过:

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

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

299146 次浏览

|=的读法与+=相同。

notification.defaults |= Notification.DEFAULT_SOUND;

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

其中|是逐位的OR运算符。

所有操作符都引用在这里

之所以使用逐位操作符,是因为通常情况下,这些常量使整型对象可以带位。

如果你在这些常量上,你会看到它们是2的幂:

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

因此,您可以使用位或来添加标志

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

所以

myFlags |= DEFAULT_LIGHTS;

简单地说就是我们添加了一个标志。

对称地,我们测试一个标志是使用&设置的:

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

它是这个的缩写:

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

|是一个逐位的OR。

|bitwise-or操作符,它像+=一样被应用。

你的问题已经有了充分的答案。但我的回答可能会帮助你更多地了解|=类型的二进制操作符。

我正在为按位运算符写表:
以下是有效的:

----------------------------------------------------------------------------------------
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中的操作符。 > > > =操作符

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

  • 另外,!调用逻辑NOT运算符,但是!=检查两个操作数的值是否相等,如果值不相等则condition为true。例如(A != B) is true。其中A=!B意味着如果Btrue,那么A就变成false(如果Bfalse,那么A就变成true)。

边注:|不被称为管道,而是被称为OR,管道是shell术语,将一个进程转移到下一个进程

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

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

y = expr ? true : y;  // expr is always evaluated.

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

特别地,当应用于一个布尔变量"|="时,当它第一次在右边遇到一个真表达式时,它将把它设置为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
< p > 编辑: 为什么这个有用?< / p > 考虑这样一种情况,您想知道在各种对象上是否有任何更改,如果有,则通知其中一个更改。因此,你将设置一个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