1) Why there is no assignment by logical operator like there is assignment by sum and difference?
bool a = true;
bool b = false;
a = a || b;
a ||= b; // syntax error!
a |= b; // OK.
2) What is the meaning of applying bitwise operator on boolean variable? Is it the same as using logical operator?
It's true that &&=
and ||=
are "missing" from C. I think one reason is that logical AND and OR in C perform short-circuiting, which would be a little strange in the abbreviated form. But don't use the bitwise assignment operators in their place. Instead, just write:
a = a && b;
c = c || d;
The bitwise operators will work if you have canonical true/false values (1 and 0). But if applied to non-canonical values, such as 5 and 2, you will get different results (5 && 2 is 1, but 5 & 2 is 0).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With