1

in the following code in java :

Notification noti = nBuilder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;

what is the this operator (|=) for ?

Adham
  • 63,550
  • 98
  • 229
  • 344

4 Answers4

7
noti.flags |= Notification.FLAG_AUTO_CANCEL;

means

noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;

where | is the Bit wise OR operator

rocketboy
  • 9,573
  • 2
  • 34
  • 36
3
  • | is the bit a bit or operator
  • |= is noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    calculates the bitwise OR of the noti.flags and Notification.FLAG_AUTO_CANCEL, and assigns the result to noti.flagsd.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

bitwise or, is the same as:

noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;

it executes an "or" operation with the bits of the operands. Say that you have

// noti.flags =                      0001011    (11 decimal)
// Notification.FLAG_AUTO_CANCEL =   1000001    (65 decimal)

// The result would be:              1001011    (75 decimal)
morgano
  • 17,210
  • 10
  • 45
  • 56
1

It's the bitwise OR with the assignment operator included. Expanded it would be noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL; Similarly you have &= for bitwise AND, ^= for bitwise XOR and ~= for bitwise NOT.

Kayaman
  • 72,141
  • 5
  • 83
  • 121