-2

Im wondering what x ^ y does in c. I wrote a little example program:

#include <stdio.h>

int main() {
    int b = (3 ^ 9);
    printf("%d\n", b);
}

The result is 10 but i dont know how to get to that solution. Con someone explain me how to do it mathematically?

BitFriends
  • 379
  • 5
  • 18
  • 2
    Did you try to read about in in any of the thousands of available C documents and tutorials? For easier searching try "bitwise XOR operator". – Eugene Sh. Mar 05 '20 at 18:51
  • 1
    I couldn't find a proper dupe target. Maybe this: https://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working – ForceBru Mar 05 '20 at 18:52

1 Answers1

3

That ^ is the XOR or exclusive-OR operator

decimal  3  binary  0011
decimal  9  binary  1001
            XOR     ----
decimal 10  binary  1010

It operates on aligned bits individually, so the result of any pair of bits in the operands does not affect any other bits.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56