Why is the expression true after bit shifting the value and condition with && in C [closed]
Have a look at the example:
unsigned char c = 64; /* 0100 0000 */
c = (c << 2 && 512); /* 512: 10 0000 0000 */
If I shift c two times to the left, I should get from 0100 0000 (64) to this 0001 0000 0000 (256). So my 8-bit char c
has only zeroes. At the end, I am wondering why the expression (c << 2 && 512)
is true (1) and not false (0), because my c has only zeroes.
Solution 1:
From the C Standard (6.5.7 Bitwise shift operators)
3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand.
So in this expression
c << 2
the object c is promoted to the type int
and the result also has the type int
.
As the both operands of the logical AND operator are not equal to 0
then the whole expression evaluates to 1
.
From the C Standard (6.5.13 Logical AND operator)
3 The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
It seems you are confusing the logical operator AND &&
with the bitwise operator AND &
.
If you will write
c = (c << 2 & 512);
then the variable c
will have the value 0.
Solution 2:
&
is a bitwise AND. Each bit in its results is the AND of the two corresponding bits in its input. The bitwise AND of 01 0000 00002 and 10 0000 00002 would be 00 0000 00002.
&&
is a logical AND. Its result is 1 if both of its operands are non-zero and 0 otherwise. The logical AND of 01 0000 00002 and 10 0000 00002 is 1.