Back to Blog
C#

C# XOR Operator: Bitwise and Logical Usage

c# xor operator: Learn how the C# XOR operator (^) works for bitwise and logical operations, with practical examples for bit toggling, flag manipulation, and common pi...

C#bitwise operatorslogical operatorsXORbit manipulation
Illustration of the C# XOR operator concept with two binary inputs and an output.

The C# XOR operator, represented by ^, serves two distinct purposes depending on the operand types. For integer types, it performs a bitwise XOR (exclusive OR). For bool operands, it performs a logical XOR. Understanding both behaviors is essential because the same symbol maps to different operations, and mixing them can lead to subtle bugs.

Bitwise XOR on Integer Types

When applied to integer types such as int, long, byte, or uint, ^ compares each bit of the left operand with the corresponding bit of the right operand. The result bit is 1 if exactly one of the two bits is 1; otherwise it is 0. This is the exclusive OR truth table:

LeftRightResult
000
011
101
110

For example:

int a = 0b1100; // 12 int b = 0b1010; // 10 int result = a ^ b; // 0b0110 = 6

Each bit pair is evaluated independently. The operation is commutative and associative, so a ^ b produces the same result as b ^ a, and (a ^ b) ^ c equals a ^ (b ^ c).

Logical XOR on Booleans

With bool operands, ^ acts as a logical exclusive OR. The result is true only when the two operands differ. This is different from || (logical OR) and && (logical AND) because those operators short-circuit and do not have an XOR equivalent.

bool x = true; bool y = false; bool xor = x ^ y; // true

The ^ operator on booleans does not short-circuit; both operands are always evaluated. That matters when an operand has a side effect, such as a method call that increments a counter or writes to a log.

Using XOR for Bit Toggling

A common practical use of bitwise XOR is to toggle specific bits in a value. Because XORing a bit with 1 flips it, and XORing with 0 leaves it unchanged, you can toggle a mask of bits in one operation:

int flags = 0b1010; int mask = 0b0100; flags ^= mask; // toggles bit 2

The compound assignment ^= applies the XOR and assigns the result back to the variable. This is useful when working with bit flags that represent independent states, such as permission sets or configuration options.

XOR Swap and Its Limitations

A classic trick uses XOR to swap two integer values without a temporary variable:

int a = 5; int b = 9; a ^= b; b ^= a; a ^= b;

After these three operations, a and b have swapped values. The technique works because XOR is its own inverse: (a ^ b) ^ b equals a. However, this approach is rarely faster than using a temporary variable on modern hardware, and it is less readable. It also fails if a and b refer to the same memory location, which can happen when swapping array elements with identical indices. In most production code, a straightforward temporary variable is clearer and safer.

Operator Precedence and Compound Assignment

The ^ operator has lower precedence than additive and multiplicative operators but higher than logical AND and OR. For example, a + b ^ c is parsed as (a + b) ^ c. When mixing bitwise and logical operators, use parentheses to make the intent explicit. The compound assignment ^= behaves like other assignment operators: it evaluates the left operand once, performs the XOR, and assigns the result.

Common Mistakes and Edge Cases

One common mistake is assuming ^ works on all numeric types. It is defined for integral types and bool, but not for float, double, or decimal. Attempting to use it on those types causes a compile-time error.

Another edge case involves signed integers. The XOR operation works on the two's complement representation, so the sign bit is treated like any other bit. For example, -1 ^ 1 results in -2 because -1 is all bits set. This is expected behavior but can surprise developers who forget that bitwise operations do not interpret the sign.

When using ^ with bool operands, remember that it does not short-circuit. If you need short-circuiting XOR behavior, you must implement it manually with a conditional expression or a helper method.

Performance and Maintainability Considerations

Bitwise XOR is a single CPU instruction and is extremely fast. However, optimizing code by replacing readable logic with XOR tricks rarely yields measurable gains unless the operation is inside a hot loop that processes millions of items. In typical application code, clarity and maintainability matter more. Use XOR where it expresses the intent naturally, such as toggling flags or checking that two conditions differ, and avoid cleverness that obscures the algorithm.

c# xor operator: Practical Usage and Code Examples | RYUSLOG DEV