我尝试使用Google搜索和Stack Overflow进行搜索,但没有显示任何结果。我在开源库代码中看到了这一点:
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
"|=“( pipe equal operator
)是什么意思?
发布于 2013-01-13 00:44:14
|=
的读取方式与+=
相同。
notification.defaults |= Notification.DEFAULT_SOUND;
等同于
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
其中|
是逐位OR运算符。
所有运算符都引用为here。
使用按位运算符是因为,这些常量通常使int能够携带标志。
如果你对这些常量执行look,你会发现它们是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
因此,您可以使用逐位OR添加标志
int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // same as 001 | 010, producing 011
所以
myFlags |= DEFAULT_LIGHTS;
简单地说,就是我们添加一个标志。
对称地,我们使用&
测试设置了一个标志:
boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;
发布于 2013-01-13 00:44:35
这是这个的缩写:
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
|
是按位OR运算。
发布于 2013-01-13 00:45:44
|
是bitwise-or运算符,它的应用与+=
类似。
https://stackoverflow.com/questions/14295469
复制相似问题