What is the difference between bitwise and logical OR operations when it comes to boolean variables in C++? In the integer domain, it is clear but when it comes to boolean is there any performance advantage or behavioral change between both of them?
bool VAR = TRUE , BOOL VAR2= TRUE
Is there a difference in VAR | VAR2 & VAR || VAR2
For performance differences, logical OR short-circuits (e.g., in
op1 || op2, ifop1evaluates totrue,op2is not evaluated), but bit-wise doesn't. This also creates a behavior difference:This code will never call
fnwithnullptrbecause of the short-circuit behavior. On the other hand, if the logical OR was replaced with bit-wise,fnwould always be called. Iffnhas side effects or undesirable behavior when called withnullptr, this creates a behavior difference.