wts*_*g02 211 java android operators
我尝试使用Google搜索和Stack Overflow搜索,但它没有显示任何结果.我在开源库代码中看到了这个:
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)
"| ="(pipe equal operator)是什么意思?
Den*_*ret 283
|=读取的方式与+=.
notification.defaults |= Notification.DEFAULT_SOUND;
Run Code Online (Sandbox Code Playgroud)
是相同的
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
Run Code Online (Sandbox Code Playgroud)
其中|是逐位OR运算符.
这里引用所有运算符.
使用逐位运算符是因为,通常,这些常量使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
Run Code Online (Sandbox Code Playgroud)
因此,您可以使用按位OR来添加标志
int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // same as 001 | 010, producing 011
Run Code Online (Sandbox Code Playgroud)
所以
myFlags |= DEFAULT_LIGHTS;
Run Code Online (Sandbox Code Playgroud)
只是意味着我们添加一个标志.
并且对称地,我们使用&以下方法测试标志:
boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;
Run Code Online (Sandbox Code Playgroud)
Gri*_*han 36
你的问题已经得到了足够的答案.但可能是我的回答可以帮助您更多地了解|=二元运算符.
我正在为按位运算符编写表:
以下是有效的:
----------------------------------------------------------------------------------------
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
----------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
注意所有运算符都是二元运算符
另请注意:( 对于以下几点,我想添加我的答案)
>>>是Java中的按位运算符,称为无符号移位
但 >>> =运营商 >>>=不是Java中的运算符.
~是按位补码位,0 to 1 and 1 to 0(一元运算符)但~=不是运算符.
另外, !调用逻辑非运算符,但!=检查两个操作数的值是否相等,如果值不相等则条件变为真.例如(A != B) is true.其中作为A=!B意味着如果B是true则A成为false(并且如果B是false则A成为true).
旁注:|不叫管道,而是叫做OR,管道是shell术语,将一个进程转移到下一个..
dbr*_*rin 15
我正在寻找关于|=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
Run Code Online (Sandbox Code Playgroud)
输出:
false
false
true
true
Run Code Online (Sandbox Code Playgroud)
编辑:为什么这有用?
考虑一种情况,您想知道各种对象上是否有任何更改,如果是,请通知其中一个更改.所以,你要设置一个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
Run Code Online (Sandbox Code Playgroud)
Til*_*lge 12
这是一个缩短:
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
Run Code Online (Sandbox Code Playgroud)
并且|是一个有点明智的OR.
注意:||= 不存在。(逻辑或)您可以使用
y= y || expr; // expr is NOT evaluated if y==true
Run Code Online (Sandbox Code Playgroud)
或者
y = expr ? true : y; // expr is always evaluated.
Run Code Online (Sandbox Code Playgroud)