Search code examples
cgccgnu

Why is 1 ^ 1 = 0 in C


In this code

#include <stdio.h>
int main(void){
    int i = 1, j = 0;

    printf("%d", i ^ !j);

    return 0;
} 

The result of this code is 0 instead of 1 even if i = 1 and j = 1.

How does it work ?


Solution

  • ^ is bitwise exclusive or in C, not exponentiation. So when you flip all the set bits of 1 in 1, you get 0.