1

How does this code work:

int a = 1;
int b = 10;

a |= b;

how the a |= b; works? Seems like |= is not an operator in C?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
user2131316
  • 3,111
  • 12
  • 39
  • 53
  • It is same as : `a = a | b`; – Alok Save Apr 22 '13 at 15:25
  • This is an elementary question, answerable by reading any decent C book or tutorial. (Operator symbols typically appear at the beginning of the index, before 'A'). – Keith Thompson Apr 22 '13 at 15:26
  • Don't downvote for no reason, please. A simple question isn't necessarily a bad question. – Neil Apr 22 '13 at 15:29
  • Does this answer your question? [What does the |= operator mean in C++?](https://stackoverflow.com/questions/4217762/what-does-the-operator-mean-in-c) – Josh Correia Aug 21 '23 at 20:53

7 Answers7

4

It works like the | + the = operator, in a similar way as += works.

It is equivalent as

a = a|b;

I suggest you to read this article about operators: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators Ans this one about bitwise operation http://en.wikipedia.org/wiki/Bitwise_operation

Antzi
  • 12,831
  • 7
  • 48
  • 74
2

Following the pattern of, for example, +=:

a |= b;
// Means the same thing as:
a = a | b;

That is, any bits that are set in either a or b shall be set in a, and those set in neither shall not be set in a.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
2

That's the "bitwise or" equal. It follows in the pattern of the plus equal +=, minus equal -=, etc.

a |= b; is the same as a = a | b;

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • "Bitwise or equal" is not the name of this operator. You are confusing it with `>=` et al., which are comparison operators. `|=` is not a comparison operator. – cdhowie Apr 22 '13 at 15:26
  • The phrasing is a bit odd, so I'll edit it. I mean "bitwise or" equal. Kinda like "plus" equal. – Mr. Llama Apr 22 '13 at 15:27
  • Ah, yes, I see what you mean now. The "or" does make it difficult to construct an operator name with the same pattern as plus-equal. – cdhowie Apr 22 '13 at 15:28
2

The expression a |= b; is equivalent to the expression a = a | b;.

mah
  • 39,056
  • 9
  • 76
  • 93
2

This is compound assignment operator. It has meaning:

a = a | b;
V-X
  • 2,979
  • 18
  • 28
2

This is the same as

a = a | b;

The same way as += -= etc

Alex
  • 9,891
  • 11
  • 53
  • 87
2

Its the bitwise OR operator, and

a |= b;

Its the same thing as

a = a | b;
fableal
  • 1,577
  • 10
  • 24