0

I just came across the following line inside some sourcecode.

int sequ |= element.sequence

What does the operator |= mean ? I haven't seen that before.

Markus
  • 1,452
  • 2
  • 21
  • 47

2 Answers2

4

=| is a compound assignment operator, similar to +=, -=, /=, or *=, but with bitwise OR instead.

This is equivalent to:

sequ = (int) (sequ | element.sequence);

where | is the bitwise OR operation, meaning that it independently ORs all bits in the left operand with those in the right operand, to get a result. The cast is not necessary if element.sequence is already an int.

Note: Your original code wouldn't make sense:

int sequ |= element.sequence

You can't declare it there and then and or it with something else. It would need to have been declared and assigned before, such as in:

int sequ = 0; /* or some other value */
sequ |= element.sequence;
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • @WalterM According to the [Java language spec](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2), `E1 op= E2` *is* `(T) ((E1) op (E2))`, which is pretty much the same thing as what I stated, possibly with a cast to the LHS type. If you were in fact talking about casting to int if element.sequence wasn't int, it would have been nice to leave a less cryptic comment. Also, to answer, "If they really were equivalent, then why would we need both?", why would we need `+=` then, if we can just assign the sum manually? – nanofarad Dec 31 '15 at 16:34
2

It is short form for:

int sequ  = sequ | element.sequence;

Similar to +=, -= except that it is bitwise OR operator.

Azodious
  • 13,752
  • 1
  • 36
  • 71