-3

So, the XOR (^) operator makes sense for boolean operators. Its basically same as !=.

Now, for int operators it makes no sense to me. I did a little test and here are my results (dont know why stackoverflow insists on formatting it like code):

10 ^ 10 = 0
10 ^ 11 = 1
10 ^ 12 = 6
10 ^ 13 = 7
10 ^ 5 = 15

Reversing operators appears to not have any effect. What is the pattern here? And, also, how could this possibly be useful in any real world application?

Paul R
  • 208,748
  • 37
  • 389
  • 560
user1219387
  • 135
  • 1
  • 12

1 Answers1

2

XOR is a bitwise operator, and its plain English description is "one or the other, but not both". Considering your truth tables, you need to look at them in a bit-wise fashion:

x y   x^y
0 0   0     <-same values, so xor is 0
1 0   1     <-values differ, so xor is 1
0 1   1     <-values differ, so xor is 1
1 1   0     <-same values, so xor is 0

So

10 decimal = 1010 binary
11 decimal = 1011 binary
12 decimal = 1100 binary
13 decimal = 1101 binary

10 ^ 11 -> 1010 ^ 1011 -> 0001 -> 1 decimal
10 ^ 12 -> 1010 ^ 1100 -> 0100 -> 6 decimal
etc...
Marc B
  • 356,200
  • 43
  • 426
  • 500