-4

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 ?

  • 4
    Because `^` is a `XOR` operation and not power. – Eugene Sh. Apr 22 '21 at 17:36
  • thx! got it.... – Saif AlHarthy Apr 22 '21 at 17:39
  • Sure. I understand now there is no exponentiation in C using operator (^). So, if you want to use power operator you have to use it mathematically. This confusion raised because I got to use Java, C++, PHP and others and I just started to use C. Thanks for your support. – Saif AlHarthy Apr 22 '21 at 17:48
  • 1
    @Saif `^` is also the XOR operator in C++, Java and PHP. Since when did you got used to those languages? – phuclv Apr 22 '21 at 17:56

1 Answers1

3

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

user3188445
  • 4,062
  • 16
  • 26