The **Exclusive or **operator, which is known as** XOR **operator is a logical boolean operator in C#.Net, the logical boolean operators have boolean operands and produce a boolean result. The caret symbol **^** in C#.Net is used as the **exclusive or (XOR)** operator.

Logical operators allow us to combine multiple boolean expressions to form a more complex boolean expression.

## Syntax & Usage

**Exclusive or (XOR) **operator requires two boolean operands to operate with. It returns **True** only if exactly one of the operand is **True **and returns **False **in all other cases. Lets look at below example to understand how it works.

1 2 3 |
bool x; bool y; bool b = x ^ y // XOR operator |

In the above example expression, we are using XOR operator, In this case the result
b will evaluate to **True** .

- If
x evaluates to
**True**and y evaluates to**False**. - If
x evaluates to
**False**and y evaluates to**True**.

In all other cases
b will evaluate to **False**. Following tabular format describes about the conditions.

Right Operand | Left Operand | Result |
---|---|---|

False | False | False |

True | False | True |

False | True | True |

True | True | False |

## Example – XOR – Exclusive OR operator (^)

Lets have a look at below example, that demonstrates above behavior.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; namespace OperatorsXOR { class Program { static void Main(string[] args) { bool x; bool y; x = true; y = true; Console.WriteLine("XOR (Exclusive OR) operator exmample"); Console.WriteLine("*******************************"); Console.WriteLine("x : {0}, y : {1} , value of x ^ y = {2}", x, y, x ^ y); x = true; y = false; Console.WriteLine("x : {0}, y : {1} , value of x ^ y = {2}", x, y, x ^ y); x = false; y = true; Console.WriteLine("x : {0}, y : {1} , value of x ^ y = {2}", x, y, x ^ y); x = false; y = false; Console.WriteLine("x : {0}, y : {1} , value of x ^ y = {2}", x, y, x ^ y); Console.Read(); } } } |

When we run above sample code then we will get below output.

In the above console window, the out put displays similar result as specified in the table listed above.

### Key Points :

- Unlike other operators, the
**Exclusive OR (^)**operator always checks both operands and determines the result. It doesn’t short-circuit.

You can go through Microsoft article on this operator here.

Thanks for checking in here, appreciate it, feel free to provide your feedback and also check my other blogs on Null Coalescing Operator (??) in C# , Null-Conditional Operator in C# (?.) and Conditional Operator in C# (?.)