> ## 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.

# C# Bitwise XOR Assignment

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

```csharp theme={"dark"}
x ^= y;
```

This expression is semantically equivalent to:

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
[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`

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
byte b = 5;
b ^= 2; 
```

Under the hood, the compiler treats the above operation as:

```csharp theme={"dark"}
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`).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
