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.

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 OperandLeft OperandResult
FalseFalseFalse
TrueFalseTrue
FalseTrueTrue
TrueTrueFalse

Example – XOR – Exclusive OR operator (^)

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

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

XOR Exclusive OR OutPut 1.0

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# (?.)