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

IT小君   2021-09-21T00:53:49

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

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

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

评论(6)
IT小君

|=读取方式与 相同+=

notification.defaults |= Notification.DEFAULT_SOUND;

是相同的

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

其中|是按位 OR 运算符。

此处引用所有运算符

使用按位运算符是因为经常使用这些常量使 int 能够携带标志。

如果您查看这些常量,您会发现它们是 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;
2021-09-21T00:53:50   回复
IT小君

你的问题已经有了足够的答案。但也许我的回答可以帮助您更多地了解|=二元运算符的种类。

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

----------------------------------------------------------------------------------------
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(一元运算符)但~=不是运算符。

  • 此外, !称为逻辑非运算符,但!=检查两个操作数的值是否相等,如果值不相等则条件为真。例如(A != B) is true其中作为A=!B手段,如果BtrueA成为false(并且如果BfalseA成为true)。

旁注:|不称为管道,而是称为 OR,管道是外壳术语,将一个进程转移到下一个进程。

2021-09-21T00:53:51   回复
IT小君

我一直在寻找关于|=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

编辑:为什么这很有用?

考虑这样一种情况,您想知道各种对象上是否有任何更改,如果是,则通知其中的某个更改。因此,您将设置一个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
2021-09-21T00:53:52   回复
IT小君

这是一个缩短:

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

并且|是按位 OR。

2021-09-21T00:53:53   回复
IT小君

|按位或运算符,它的应用类似于+=

2021-09-21T00:53:54   回复
IT小君

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

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

或者

y = expr ? true : y;  // expr is always evaluated.
2021-09-21T00:53:54   回复