[英]What does the |= operator do in Java?
While reading the Android guide to Notifications , I stumbled across this:在阅读Android 通知指南时,我偶然发现了这一点:
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:
要使用默认模式,请将“DEFAULT_VIBRATE”添加到默认字段:
notification.defaults |= Notification.DEFAULT_VIBRATE;
What this does is clear: it adds the DEFAULT_VIBRATE
flag to the default flags of the notification object.它的作用很明显:它将
DEFAULT_VIBRATE
标志添加到通知对象的默认标志中。 But what does the |=
operator do in Java?但是
|=
运算符在 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).它采用 LHS 的当前值,按位或 RHS,并将该值分配回 LHS(与
+=
加法方式类似)。
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
a |= x
is a = a | x
a |= x
是a = a | x
a = a | x
, and |
a = a | x
, 和|
is "bitwise inclusive OR"是“按位包含或”
Whenever such questions arise, check the official tutorial on operators .每当出现此类问题时,请查看有关运算符的官方教程。
Each operator has an assignment form:每个运算符都有一个赋值形式:
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
>>>=
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
>>>=
Where a OP= x
is translated to a = a OP x
其中
a OP= x
被转换为a = a OP x
And about bitwise operations :关于按位运算:
0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)
The bitwise OR may be used in situations where a set of bits are used as flags;
在使用一组位作为标志的情况下,可以使用按位或; the bits in a single binary numeral may each represent a distinct Boolean variable.
单个二进制数字中的位每个都可以代表一个不同的布尔变量。 Applying the bitwise OR operation to the numeral along with a bit pattern containing 1 in some positions will result in a new numeral with those bits set.
将按位或运算应用于数字以及在某些位置包含 1 的位模式将产生一个新的数字,这些位被设置。
It is a short hand notation for performing a bitwise OR and an assignment in one step.它是一种用于在一个步骤中执行按位 OR 和赋值的简写符号。
x |= y
is equivalent to x = x | y
x |= y
等价于x = x | y
x = x | y
This can be done with many operators, for example:这可以通过许多运算符来完成,例如:
x += y
x -= y
x /= y
x *= y
etc.
An example of the bitwise OR using numbers.. if either bit is set in the operands the bit will be set in the result.使用数字的按位或的示例.. 如果在操作数中设置了任一位,则将在结果中设置该位。 So, if:
因此,如果:
x = 0001 and
y = 1100 then
--------
r = 1101
In this case, notification.defaults
is a bit array .在这种情况下,
notification.defaults
是一个位数组。 By using |=
, you're adding Notification.DEFAULT_VIBRATE
to the set of default options.通过使用
|=
,您将Notification.DEFAULT_VIBRATE
添加到默认选项集。 Inside Notification
, it is likely that the presence of this particular value will be checked for like so:在
Notification
,很可能会像这样检查这个特定值的存在:
notification.defaults & Notification.DEFAULT_VIBRATE != 0 // Present
This is the bit wise OR operator.这是位明智的 OR 运算符。 If notifications.default is 0b00000001 in binary form and Notification.DEFAULT_VIBRATE is 0b11000000, then the result will be 0b11000001.
如果notifications.default 是二进制形式的0b00000001 并且Notification.DEFAULT_VIBRATE 是0b11000000,那么结果将是0b11000001。
按位或运算符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.