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

The `^` operator in C# serves two distinct syntactic functions depending on its context: as a binary **Logical/Bitwise Exclusive OR (XOR)** operator, and as a unary **Index from End** operator.

## 1. Logical and Bitwise Exclusive OR (XOR)

As a binary operator, `^` evaluates the exclusive OR of its operands. It is supported for boolean types and integral numeric types.

**Boolean Operands:**
When applied to `bool` operands, `^` computes the logical exclusive OR. It evaluates to `true` if and only if exactly one operand evaluates to `true`. If both operands evaluate to the same value, the result is `false`. Unlike the conditional logical operators (`&&`, `||`), the `^` operator always evaluates both operands; it does not short-circuit.

```csharp theme={"dark"}
bool result1 = true ^ false;  // Evaluates to true
bool result2 = true ^ true;   // Evaluates to false
bool result3 = false ^ false; // Evaluates to false
```

**Integral Numeric Operands:**
When applied to integral types (`int`, `uint`, `long`, `ulong`, `byte`, etc.), `^` computes the bitwise exclusive OR. It compares the binary representation of both operands bit by bit. The resulting bit is set to `1` if the corresponding bits in the operands are different, and `0` if they are identical.

```csharp theme={"dark"}
int a = 5;     // Binary: 0000 0101
int b = 3;     // Binary: 0000 0011
int c = a ^ b; // Binary: 0000 0110 (Evaluates to 6)
```

**Compound Assignment:**
The `^` operator can be combined with the assignment operator to form `^=`. This applies the XOR operation to the left and right operands and assigns the resulting value to the left-hand variable.

```csharp theme={"dark"}
int x = 5;
x ^= 3; // Equivalent to: x = x ^ 3; (x evaluates to 6)
```

## 2. Index from End Operator (C# 8.0+)

As a unary prefix operator, `^` instantiates a `System.Index` struct representing an offset relative to the end of a sequence (such as an array, `Span<T>`, or `string`).

The index `^n` is mathematically translated by the compiler to `sequence.Length - n`. Because C# uses zero-based indexing from the start, the index `^0` equals the sequence length (which throws an `IndexOutOfRangeException` if used for direct element access), and `^1` represents the final element in the sequence.

```csharp theme={"dark"}
int[] numbers = { 10, 20, 30, 40, 50 };

// Evaluates to numbers[numbers.Length - 1]
int lastElement = numbers[^1];      // 50

// Evaluates to numbers[numbers.Length - 2]
int secondToLast = numbers[^2];     // 40

// Explicitly instantiating a System.Index type
Index indexFromEnd = ^3;            
int middleElement = numbers[indexFromEnd]; // 30
```

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