4

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.

anass
  • 165
  • 1
  • 8

6 Answers6

7

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

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
  • Thank you this is what i was looking for. I already read the android doc but i did not understand but now it's better :p – anass Jul 05 '13 at 10:38
  • Honnestly i did wrong operation i thought that 0x04000000 | 0x20000000= 0x60000000 So i did not pay attention to the position of the 4 x) – anass Jul 05 '13 at 10:43
2

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 +=.

Nirali
  • 13,571
  • 6
  • 40
  • 53
2

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.

Eli Turchinsky
  • 331
  • 2
  • 8
1

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

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.

das_j
  • 4,444
  • 5
  • 31
  • 47
1

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.

kutschkem
  • 7,826
  • 3
  • 21
  • 56