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
  • 61,516
  • 96
  • 221
  • 343

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,277
  • 1
  • 33
  • 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
  • 152,872
  • 27
  • 286
  • 297
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
  • 16,937
  • 9
  • 46
  • 54
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
  • 70,361
  • 5
  • 75
  • 119