| =运算符在Java中做什么? [英] What does the |= operator do in Java?

查看:137
本文介绍了| =运算符在Java中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 Android通知指南时,我偶然发现:


添加振动



您可以使用默认振动模式或应用程序定义的
振动模式提醒用户。



要使用默认模式,请添加DEFAULT_VIBRATE到默认值字段:

  notification.defaults | = Notification.DEFAULT_VIBRATE; 


这样做很清楚:它增加了 DEFAULT_VIBRATE 标记通知对象的默认标志。
| = 运算符在Java中是做什么用的?
它看起来像一个OR,但它是如何工作的?



你能提供一个使用数字的例子吗?



谢谢

解决方案

| = 是一个按位OR赋值运算符。它采用LHS的当前值,按位或者RHS,并将值分配回LHS(与 + = 类似的方式添加)。 / p>

例如:

  foo = 32; // 32 = 0b00100000 
bar = 9; // 9 = 0b00001001
baz = 10; // 10 = 0b00001010
foo | = bar; // 32 | 9 = 0b00101001 = 41
//现在foo = 41
foo | = baz; // 41 | 10 = 0b00101011 = 43
//现在foo = 43


While reading the Android guide to Notifications, I stumbled across this:

Adding vibration

You can alert the user with the the default vibration pattern or with a vibration pattern defined by your application.

To use the default pattern, add "DEFAULT_VIBRATE" to the defaults field:

notification.defaults |= Notification.DEFAULT_VIBRATE;

What this does is clear: it adds the DEFAULT_VIBRATE flag to the default flags of the notification object. But what does the |= operator do in Java? It looks like an "OR", but how does it work?

Can you provide an example using numbers?

Thanks

解决方案

|= is a bitwise-OR-assignment operator. It takes the current value of the LHS, bitwise-ors the RHS, and assigns the value back to the LHS (in a similar fashion to += does with addition).

For example:

foo = 32;   // 32 =      0b00100000
bar = 9;    //  9 =      0b00001001
baz = 10;   // 10 =      0b00001010
foo |= bar; // 32 | 9  = 0b00101001 = 41
            // now foo = 41
foo |= baz; // 41 | 10 = 0b00101011 = 43
            // now foo = 43

这篇关于| =运算符在Java中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆