Search code examples
cbitwise-operatorsbitwise-and

Bitwise operation producing unexpected output


#include<stdio.h>
int main(void)
{
    int a=0x11;
    printf("\n %d",a);
    int b=10;
    int c=(a&b);
    printf("\t %d",c);
    return 0;
}

The o/p of this program is

17 0

But I expected the program to operate on bits such that it results in

17 16

Why the output is 0?


Solution

  • In binary notation 0x11 is

    0001 0001
    

    while 10 is

    0000 1010
    

    As you can easily see, these values have no common 1 bits. For this reason, the result of bitwise-and operation between these values is 0. Why did you expect it to produce 16?