What the different between logical operators and
, or
and bitwise analogs &
, |
in usage? Is there any difference in efficiency in various solutions?
Logical operators: Compare bits of the given object and always return a Boolean result. Bitwise operators: Perform operations on individual bits, and the result is also always a bit. Assignment operators: allow us to initialize an object with a value or perform specific operations on it.
Bitwise operators vs Logical operators in CThe logical operators work with Boolean data and return a Boolean value, i.e. True or False. The bitwise operators in C work with integers, i.e. they take integer inputs, manipulate with their bit and return an integer value.
No. First, using bitwise operators in contrast to logical operators is prone to error (e.g., doing a right-shift by 1 is NOT equivalent to multiplying by two).
“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers.
Logical operators operate on logical values, while bitwise operators operate on integer bits. Stop thinking about performance, and use them for they're meant for.
if x and y: # logical operation
...
z = z & 0xFF # bitwise operation
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