It's always executed. Note that a |= b
is indeed a shorthand for a = a | b
(only evaluating a
once). In particular, it's not a shorthand for a = a || b
.
This means it does not provide the short-circuiting behaviour of boolean operators, so the b
is always evaluated.
Using these shorthand assignment forms with bool
variables is dangerous, precisely because the sematnics are non-obvious. &=
is actually even worse. Compare this:
int two() { return 2; }
int main()
{
bool b = true;
b = b && two();
assert(b); //OK
}
with this:
int two() { return 2; }
int main()
{
bool b = true;
b &= two();
assert(b); //FAILS!!
// b &= two(); was actually b = 1 & 2, which is 0 !
}
In short, avoid using |=
and &=
with boolean variables.