Reputation: 165
I have found this line in an application source code but i cant figure out the meaning of the bitwise or inclusive operator "|" between the two flags.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
I did not also understand the meaning of this operator |= in the following line:
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Someone could help me plz.
Upvotes: 4
Views: 659
Reputation: 331
Let's for assume for example that FLAG_ACTIVITY_CLEAR_TOP is 2 and FLAG_ACTIVITY_SINGLE_TOP is 4. So in binnary the represantion will be 0000000010 for the decimal value 2 and 00000100 for the value 4. The binary or operation between those two values will give the value 6 : 00000110 (The on bits on both 2 and 4 are on) . using power of two values for suck constants will make sure thatonly unique values will come out after th bitwise or:
For example : 1 is 00000001 2 is 00000010 4 is 00000100 8 is 00001000 16 is 00010000 .....
If you are settings flags this way- it's very easy to decode the original flags : just perform a bitwise AND operation with the original flag , if it's zero than the flag is not there - if it's the flag itself - then the flag is up.
For example : let's check 000011000 for the flag SOME_FLAG - and let's say for the porpuse of the example that it's value is 8 - 00001000. After the bitwise and operation : 00011000 & 00001000 - We will get 00001000 , ANDing with something else (that does not include the flag SOME_FLAG - like any other flag with a power of 2 value) will return 0.
Upvotes: 2
Reputation: 13805
a | b is bitwise OR of a and b.
Its the Assignment by bitwise OR
a1 |= a2;
is short for:
a1 = a1 | a2;
|= reads the same way as +=.
Upvotes: 2
Reputation: 3408
I'd started my answer while no-one else had answered, so I decided to finish it anyway...
The pipe and ampersand | and &
perform the OR
and AND
operations respectively.
You'll be used to seeing ||
and &&
, which perform the boolean logic OR
and AND
, and the use of a single | or &
is a bitwise operation.
If you look on the flag documentation, the flag for clear_top is 0x04000000, and single_top is 0x20000000.
The operation which you are performing is therefore: 0x04000000 OR 0x20000000 = 0x24000000
Which sets the required bits in the intent to use both of the desired flags.
The a |= b
operator is the overloaded equivalent of a = a | b
, similar to the usage of +=
, --
or ++
, which you should be used to seeing elsewhere
Upvotes: 7
Reputation: 8163
If you look at those flags, you will see they are all powers of two. This means exactly one bit is set to 1, so performing a bitwise or in this case does just mean to set all those flags.
Upvotes: 1
Reputation: 4794
As far as I know, these are bit operators.
As Bathsheba wrote, it's equal to (notification.flags | Notification.FLAG_AUTO_CANCEL);
It's an logical or, for informations see here: Oracle.com
Infos about or at Wikipedia.
Upvotes: 1
Reputation: 234715
It's the same as notification.flags = (notification.flags | Notification.FLAG_AUTO_CANCEL);
cf. a += b
is equivalent to a = (a + b);
where I've used the superfluous parentheses for clarity.
Upvotes: 1