繁体   English   中英

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

[英]What does “|=” mean? (pipe equal operator)

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

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

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

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

notification.defaults |= Notification.DEFAULT_SOUND;

是相同的

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

哪里| 是按位运算符。

这里引用所有运算符。

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

如果你一下这些常数,你就会发现它们的权力是两个:

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;

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

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

----------------------------------------------------------------------------------------
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运算符,但是!=检查两个操作数的值是否相等,如果值不相等则条件变为真。 例如(A != B) is true 其中A=!B表示如果BtrueA变为false (如果BfalseA变为true )。

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

我一直在寻找一个答案是什么|=确实在Groovy虽然上面的答案是正确的,他们并没有帮助我了解一个特定的代码块我一直在寻找。

特别是,当应用于布尔变量时,“| =”将在第一次遇到右侧的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

这是一个缩短:

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

并且| 是一个有点OR。

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

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

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM