|=
I'm curious to learn about this operator, I've seen this notation used while setting flags in Java.
for example:
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Does it perform some kind of bit manipulation?
What does this mark exactly do?
Are there any other well known signs similar to this?
It is called bitwise or operator. For example,
So, this concept is used when a same option can use multiple values.
As an example, consider a variable flags equal to one.
Now, if a flag value of 4 is added to it with bitwise or,
You can determine whether 4 was applied on flags with the bitwise and.
If that flag has been applied on the flags, bitwise and returns flag. In this way we can use bitwise and & bitwise or.
Hope this helps.
If you use
|
with numeric operands then yes, it will be bitwise OR, if you use it on boolean operands it will be logical (non-short-circuit) OR. ExampleLogical OR: lets say that
1
representstrue
and0
false
Bitwise OR will perform similar operation as boolean but will use corresponding bits
x |= y
is the same asx = x | y
so it will calculatex | y
and store it inx
.Yes, every arithmetic, bitwise or bit shift operator can be used this way:
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
>>>=
Here are some additional informations about usage of
|=
inLets say that we have five properties.
We can use last five bits of number to represents situations where we have (1) or don't have (0) some property.
Now, lets say that we want to use only properties 1, 2 and 4. To do this we have to set bits indexed with 0, 1 and 3 to value 1 like
In other words we have to produce number 13 (= **1***8 + **1***4 + **0***2 + **1***1). We can do this with
|
OR bitwise operator8|4|1
becauseBut to avoid magic numbers we can create constants that will represents our properties in bit world. So we can create
and use it later like
which is more readable than
int context = 8|4|1
.Now if we want to change our context and lets say add
PROPERTY_3
we can useor shorter version based on compound assignments operators
which will do this calculations
(main difference between adding value of
PROPERTY_3
tocontext
and using bitwise OR|
is that whencontext
will already havePROPERTY_3
set to1
then OR will no effect it).Now if you take a look here idea of using single bits as flags for properties was used in
Notification
class, whereFLAG_AUTO_CANCEL
constant has value16
(0x010
in hexadecimal,0b0001_0000
in binary).Yes. It "OR"s the right-hand-side operand into the left-hand-side one.
It's an assignment coupled with an OR.
There are plenty:
+=
,-=
,*=
,/=
,%=
,&=
, and so on. Collectively, they are called compound assignment operators. They are described in section 15.26.2 of the Java Language Specification.It is equivalent to
where
|
is bitwise OR operator which OR the two variables bit-by-bit.It is well known by itself. There are also
+=
,-=
,*=
,/=
,%=
,&=
,^=
.This syntax is also available in C/C++ and other languages. It is a bitwise OR and the same as:
and similar to other operators like addition, subtraction, etc. For example:
is the same as: