C++ has logical operations, !a
, a && b
, a || b
applicable to true/false values.
And it has bitwise operations, applicable to all (integer?) types, ~a
, a & b
, a | b
, a ^ b
, (a << N
, a >> N
).
Together with in place mutation operators, a &= b
, a |= b
, a ^= b
, (a <<= b
, a >>= b
).
What confuses me is that bitwise operations could be acting on bits of bool
that are not at all part of the true/false value.
Even doing unnecessary bit operations.
I guess that last fact makes bitwise shifting undefined for bool
and that makes me doubt whether all bitwise operations are valid at all.
So, are these equivalent for bool types? ~a == !a
, a && b == a & b
, a || b == a | b
?
For some reason (won't question here) there is no "in-place" logical mutation operators (i.e. a &&= b
, a ||= b
).
This inspires these questions, are these other bitwise operations valid on bools themselves? are a &= b
and a |= b
always equivalent to a = a && b
and a = a || b
respectively?** (no "in-place" xor a = a ^ b
?)
Some examples: https://godbolt.org/z/hcccG8c9o