25

What does the |= operator mean in C++?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Jeff
  • 12,147
  • 10
  • 51
  • 87

3 Answers3

44

Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:

a = a | b;
a |= b;

The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.

The big advantage of the '|=' operator is when 'a' is itself a complex expression:

something[i].array[j]->bitfield |= 23;

vs:

something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;

Was that difference intentional or accidental?

...

Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:

something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;

Similar comments apply to all of the compound assignment operators:

+= -= *= /= %=
&= |= ^=
<<= >>=

Any compound operator expression:

a XX= b

is equivalent to:

a = (a) XX (b);

except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    although a particularly evil class could implement `|=` to do whatever the heck it wanted. Usually, it would implement `|` and `|=` in the intuitive way you show here. – Steve Townsend Nov 18 '10 at 17:35
  • 1
    @zzzzBov: yes - there's always that caveat in C++; but if 'a' and 'b' are integers, then I think you're safe. And if you work with classes where the overloads are wonky and don't work as expected, it is time to get the bug fixed or use different classes. – Jonathan Leffler Nov 18 '10 at 17:36
  • 'except that a is evaluated just once' + 1 – Chubsdad Nov 19 '10 at 03:58
  • Another possible advantage: `something[i++].array[--j]->bitfield |= 23;` is well-defined; you couldn't do the increment or decrement if it was written out longhand. OTOH, I'm not sure that such a complex expression is remotely a good idea - but a simpler one like `array[i++] |= 0x3C;` is perfectly plausible. – Jonathan Leffler Jun 10 '11 at 13:52
  • @JonathanLeffler: [Boost Serialization](http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/index.html) uses `&` as their IO operator, to be similar but different from streams. – Mooing Duck Jan 18 '12 at 20:34
  • @MooingDuck: Thanks for the information. I'm not sure whether the fact that the library uses & for I/O is a commendation for the library; I will reserve judgement, but note that it does not feel comfortable. – Jonathan Leffler Jan 18 '12 at 21:56
  • @JonathanLeffler: I frown at it, but would (like you) want to find out why before I took that opinion strongly. – Mooing Duck Jan 18 '12 at 22:27
10
x |= y

same as

x = x | y

same as

x = x [BITWISE OR] y
ronag
  • 49,529
  • 25
  • 126
  • 221
4

It is a bitwise OR compound assignment.

In the same way as you can write x += y to mean x = x + y

you can write x |= y to mean x = x | y, which ORs together all the bits of x and y and then places the result in x.

Beware that it can be overloaded, but for basic types you should be ok :-)

Lee Netherton
  • 21,347
  • 12
  • 68
  • 102