Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does mean these two operator "|=" and "|"

Tags:

java

android

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.

like image 977
anass Avatar asked Dec 16 '22 08:12

anass


1 Answers

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

like image 54
Matt Taylor Avatar answered Jan 01 '23 03:01

Matt Taylor