10

Can anyone explain this operator with a good example?

I know what this operator is. I mean a real-life example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57

6 Answers6

18

It is an implementation of the the logical operation exclusive disjunction

http://en.wikipedia.org/wiki/Exclusive_or

Exclusive disjunction is often used for bitwise operations. Examples:

  • 1 xor 1 = 0
  • 1 xor 0 = 1
  • 0 xor 1 = 1
  • 0 xor 0 = 0
  • 1110 xor 1001 = 0111 (this is equivalent to addition without carry)

As noted above, since exclusive disjunction is identical to addition modulo 2, the bitwise exclusive disjunction of two n-bit strings is identical to the standard vector of addition in the vector space (Z/2Z)^4.

In computer science, exclusive disjunction has several uses:

  • It tells whether two bits are unequal.
  • It is an optional bit-flipper (the deciding input chooses whether to invert the data input).
  • It tells whether there is an odd number of 1 bits ( is true iff an odd number of the variables are true).

(and a whole ton of other uses)

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
14

For example, like this:

var result = a ^ b;

result          a        b
--------------------------------
true            true    false
true            false   true
false           true    true
false           false   false
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
7

For "exclusive or" to evaluate to true one and only one operand has to be true.

foo ^ bar

is equivalent to

(foo && !bar) || (!foo && bar)
Till
  • 3,084
  • 17
  • 18
  • and @shenhengbin your answers about evaluating boolean values with the exclusive or operator made me wonder: isn't doing `foo ^ bar` equivalent to `foo != bar` as well then? – T_D Apr 27 '20 at 15:37
2

When using XOR, the statement only evaluates to true if only ONE of the compared statements is true. So:

bool foo = true;
bool bar = false;
if (foo ^ bar) { bar = true;} // this evaluates to true 
bool baz = foo ^ bar; // This evaluates to false, since both statements are now true. 
gehho
  • 9,049
  • 3
  • 45
  • 59
Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65
1

A programming language's reference is always the best place to look for the definitions of operators.

In this case, MSDN is the most appropriate definition for a C# operator.

According to the documentation:

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

An example is also listed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kynth
  • 2,597
  • 1
  • 18
  • 24
1

XOR is a common boolean operator and has nothing unique to it in C#. I suggest reading a little about boolean algebra to learn what it is used for with 1 bit, then check what you get when you do (a XOR b) XOR b with any two numbers or characters a and b.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111