What is the difference between Bitwise AND & and LOGICAL AND &&
&
modifies integers with bitwise operations, ie. 1000 & 1001 = 1000
, &&
compares boolean values. However, &
doubles as the non-shortcircuiting logical and, meaning if you have false & true
, the second parameter would still be evaluated. This won't be the case with &&
.
Bitwise, as its name implies, it's an AND operation at the BIT level.
So, if you perform a BITWISE AND on two integers:
int a = 7; // b00000111
int b = 3; // b00000011
int c = a & b; // b00000011 (bitwise and)
On the other hand, in C#, logical AND operates at logical (boolean) level. So you need boolean values as operators, and result is another logical value:
bool a = true;
bool b = false;
bool c = a && b; // c is false
c = a && true; // c is true
But only at the logical level.
Well, Good question (duplicated though).
Bitwise AND will affect its operators on the bit-level i.e. looping and doing logical AND operation on every bit.
On the other hand,
Logical AND will take 2 boolean operators to check their rightness (as a whole) and decide upon (notice that bool in C# is 2 bytes long).