I tried searching using Google Search and Stack Overflow, but it didn’t show up any results. I have seen this in opensource library code:
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
What does “|=” ( pipe equal operator ) mean?
|=reads the same way as+=.is the same as
where
|is the bit-wise OR operator.All operators are referenced here.
A bit-wise operator is used because, as is frequent, those constants enable an int to carry flags.
If you look at those constants, you’ll see that they’re in powers of two :
So you can use bit-wise OR to add flags
so
simply means we add a flag.
And symmetrically, we test a flag is set using
&: