Why is the expression true after bit shifting the value and condition with && in C

Viewed 104

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.

3 Answers

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.

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

The answer by Vlad is the explanation.
Here is a visualisation of how hard it is to get something into a special data type, in the presence of promotion:

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    
    unsigned char c = 64;
    
    if (c <<   2 ) {printf("Yes 1.\n");}
    if (c << '\2') {printf("Yes 2.\n");}
    if ((unsigned char)(c << 2)) {printf("Yes 3.\n");} else {printf(" No 3.\n");}
    if ((unsigned char)(c) << (unsigned char)(2)) {printf("Yes 4.\n");} else {printf(" No 4.\n");}
    

    return 0;
}

Output (e.g. here https://www.tutorialspoint.com/compile_c_online.php ):

Hello, World!
Yes 1.
Yes 2.
 No 3.
Yes 4.

The version from your code fails in 1.
A version attempting to use unsigned char operands fails in 2.
Only 3, an explicit cast after the operation manages to override the promotion.
While 4, attempting to use explicit cast on the operands before the operation fails again.

Related