Is there any functional difference between logical and bitwise operators in the following code? What are the reasons to use one or another?
typedef unsigned char BOOLEAN;
void orOperatorsComparison(BOOLEAN bBar, BOOLEAN bFoo)
{
BOOLEAN bLogicalOr = (bFoo || bBar);
BOOLEAN bBitwiseOr = (bFoo | bBar);
...
}
Difference Between Bitwise and Logical Operators First, logical operators work on boolean expressions and return boolean values (either true or false), whereas bitwise operators work on binary digits of integer values (long, int, short, char, and byte) and return an integer.
The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data.
A Bitwise And operator is represented as '&' and a logical operator is represented as '&&'.
“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers. Languages like C/C++ and python do auto promotion of boolean to integer type when an integer operator is applied.
The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data. The && operator does not evaluate second operand if first operand becomes false.
As we know the bit-wise AND is represented as ‘&’ and the logical operator is represented as ‘&&’. There are some fundamental differences between them. These are as follows − The logical AND operator works on Boolean expressions, and returns Boolean values only.
Logical and Bitwise Operators in Visual Basic 1 Unary Logical Operator. The Not Operator performs logical negation on a Boolean expression. ... 2 Binary Logical Operators. The And Operator performs logical conjunction on two Boolean expressions. ... 3 Short-Circuiting Logical Operations. ... 4 Bitwise Operations. ...
The following list orders bitwise and shift operators starting from the highest precedence to the lowest: 1 Bitwise complement operator ~ 2 Shift operators << and >> 3 Logical AND operator & 4 Logical exclusive OR operator ^ 5 Logical OR operator | More ...
What does "support" mean?
If it's a logical or that you mean, then of course you should always use ||
since that is the Boolean, logical, "or" operator.
It has the benefit of being able to short-circuit, but that won't matter much in code this simple.
I would consider it odd and weird (and due for correcting) if bitwise or was being used when the point is not to manipulate bits.
The boolean || will short-circuit: if the first operand is true
, the second will never be evaluated. In contrast, the bitwise | always evaluates both arguments.
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