Quote:
|
Originally Posted by Atheist
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
|
Absolutely
NOT TRUE. Again as I've said in previous posts, if you do not know what you're talking about, do not pretend that you do and make untrue assertions. It's best to keep quiet and not confuse people who may believe what you post.
The two are only interchangable here because both expressions of the operand are bits. If they weren't both bits you could get different results with the two operators.
Bitwise AND looks at each bit of the two numbers and does a logical AND on the corresponding bits. Bits in the result are 1 if the corresponding bits in the two operands are both 1, else the bits are 0.
Logical AND looks at the whole expression on each side. If both expressions are nonzero then the AND expression evaluates to TRUE (the value 1 in C).
This snippet of code:
Code:
int a = 0x03;
int b = 0x06;
printf("%d %d\n", (a && b), (a & b));
prints:
Why? (a && b) evaluates to TRUE because both numbers are nonzero, so we see a 1. (a & b) in binary is (0011 & 1010) and it evaluates to 0010, or 2.