Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical vs bitwise

What the different between logical operators and, or and bitwise analogs &, | in usage? Is there any difference in efficiency in various solutions?

like image 789
I159 Avatar asked Dec 07 '11 15:12

I159


People also ask

What is logical and bitwise operators?

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.

What is the difference between logical operator and bitwise operator in C?

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.

Is bitwise or faster than logical or?

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).

What is the difference between bitwise not and logical not?

“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers.


1 Answers

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
like image 177
Cat Plus Plus Avatar answered Nov 15 '22 16:11

Cat Plus Plus