cbitwise-operatorstilde

Why the if statement is false as ~0 is 1 which is 1==1 should be true?


#include <stdio.h>

int main() {
    if (~0 == 1)  
        printf("yes\n");
    else
        printf("no\n");
}

why is the if statement false? can anyone explain?


Solution

  • ~ Binary One's Complement Operator is unary and has the effect of 'flipping' bits.

    So when you do ~0 == 1 it will check for -1 == 1 which is false