&是什么QUOT; | ="意味着? (管等于运算符) [英] What does "|=" mean? (pipe equal operator)
问题描述
我尝试使用谷歌搜索和堆栈&NBSP搜索,溢出,但它并没有出现任何结果。我已经看到了这样的开源库code:
I tried searching using Google Search and Stack Overflow, but it didn't show up any results. I have seen this in opensource library code:
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
什么是| =?(管等于运营商
)的意思
推荐答案
| =
读取相同的方式 + =
。
notification.defaults |= Notification.DEFAULT_SOUND;
是相同的
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
其中, |
是位OR操作符
所有的运营商都在这里参考。
All operators are referenced here.
逐位运算符是因为,作为频繁,这些常量使一个int携带国旗。
A bit-wise operator is used because, as is frequent, those constants enable an int to carry flags.
如果您look那些常量,你会看到,他们是在两个大国:
If you look at those constants, you'll see that they're in powers of two :
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
所以,你可以使用逐位或添加标记
So you can use bit-wise OR to add flags
int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // makes 001 | 100, which is 101
所以
myFlags |= DEFAULT_LIGHTS;
只是意味着我们添加一个标志。
simply means we add a flag.
和对称,我们测试的一个标志设置使用&安培;
:
And symmetrically, we test a flag is set using &
:
boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;
这篇关于&是什么QUOT; | =&QUOT;意味着? (管等于运算符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!