Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ^= operator is the compound assignment operator for the exclusive OR (XOR) operation. It evaluates the XOR operation between the left-hand and right-hand operands, and subsequently assigns the resulting value back to the left-hand operand.

Syntax

x ^= y;
This expression is semantically equivalent to:
x = x ^ y;
The critical distinction is that in the compound assignment (^=), the left-hand operand x is evaluated only once. This is significant when x is a property access or an indexer with side effects (e.g., array[GetIndex()] ^= value;), ensuring the side effect occurs only one time.

Operational Mechanics

The behavior of the ^= operator depends strictly on the data types of its operands. It applies to integral numeric types, booleans, enumerations, and types implementing specific operator overloads or generic interfaces.

Bitwise XOR (Integral and Enumeration Types)

When applied to integral numeric types (int, uint, long, ulong, short, ushort, byte, sbyte, char, nint, nuint), the operator performs a bitwise XOR on the binary representations of the operands. For each corresponding bit position, the operator yields:
  • 1 if the bits are different.
  • 0 if the bits are identical.
int a = 10;      // Binary: 1010
a ^= 6;          // Binary: 0110
                 // ----------
                 // Result: 1100 (Decimal: 12)
When applied to enumeration types (enum), including those decorated with the [Flags] attribute, the operator performs the bitwise XOR operation on the underlying integral type of the enumeration.
[Flags]
enum Status { None = 0, Active = 1, Error = 2 }

Status current = Status.Active;
current ^= Status.Error; // Evaluates to Status.Active | Status.Error (3)

Logical XOR (Boolean Types)

When applied to bool operands, the operator performs a logical strict disjunction. It evaluates to true if and only if the operands have different boolean values.
  • true ^= false yields true
  • false ^= true yields true
  • true ^= true yields false
  • false ^= false yields false
bool isReady = true;
isReady ^= false;    // isReady remains true
isReady ^= true;     // isReady becomes false

Operator Overloading and Generic Math

The ^= operator itself cannot be explicitly overloaded. However, if a user-defined type (class or struct) overloads the binary ^ operator, the ^= compound assignment is automatically supported and evaluated using that overload. The left-hand operand is reassigned to the new instance returned by the overloaded operator.
public struct CustomBitSet
{
    public int Value { get; }
    public CustomBitSet(int value) => Value = value;
    
    // Overloading ^ automatically enables ^=
    public static CustomBitSet operator ^(CustomBitSet left, CustomBitSet right) => 
        new CustomBitSet(left.Value ^ right.Value);
}

CustomBitSet set1 = new CustomBitSet(5);
set1 ^= new CustomBitSet(3); // set1 is reassigned to the new CustomBitSet instance
Additionally, as of C# 11, the ^= operator is natively supported by any type that implements the IBitwiseOperators<TSelf, TOther, TResult> interface via Generic Math, allowing the operator to be used in generic contexts where the type parameter is constrained to this interface.

Type Promotion and Casting

When using ^= with smaller integral types (like byte or short), C# applies implicit numeric promotion. The operands are widened to int before the XOR operation occurs. The ^= operator automatically handles the required explicit cast back to the original type of the left-hand operand, provided the operation is valid.
byte b = 5;
b ^= 2; 
Under the hood, the compiler treats the above operation as:
b = (byte)(b ^ 2);
This prevents the compilation error that would normally occur if you manually wrote b = b ^ 2; (since b ^ 2 evaluates to an int and cannot be implicitly converted back to a byte).
Master C# with Deep Grasping Methodology!Learn More