I had a small query about the difference between “Logical Operators” and “Bitwise Operators”. So, I was solving a problem where I had to use “and” operator and I had used “&”, which also represents ‘and’. It seemed to be working for the first 2 expressions but didn’t work for the third one and when I changed it to logical, it did work. I looked up for some explanations online, but didn’t help much. Could somebody help me?
code:
Logical
AND
is represented by&&
Bitwise
AND
is represented by&
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.Example:
Output:
The
&&
operator does not evaluate second operand if first operand becomes false. Similarly,||
operator does not evaluate second operand when first one becomes true, but bitwise operators like&
and|
always evaluate their operands.Example:
Output:
Hope you understood
Following. Ustai huna parni? Why different? Hopeing to read good answer.
Hope This will help you.
https://www.tutorialspoint.com/what-are-the-differences-between-bitwise-and-logical-and-operators-in-c-cplusplus#:~:text=The%20logical%20AND%20operator%20works,returns%20that%20type%20of%20data.
There is a significant difference between bitwise and logical operators. Bitwise operators perform an operation on the bits of a number, while logical operators perform an operation on the truth value of expressions.
Bitwise operators are typically used for bit manipulation, such as setting, clearing, or toggling individual bits in a number. Logical operators are typically used for Boolean operations, such as AND, OR, and NOT.