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

The `^` operator in C# serves two distinct syntactic roles 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) Operator

When used as a binary operator between two operands, `^` computes the exclusive OR. The evaluation mechanics depend strictly on the operand types:

* **Boolean Operands (`bool`):** Performs a logical XOR. It evaluates to `true` if and only if exactly one operand is `true`. If both operands share the same boolean value, it evaluates to `false`.
* **Integral Operands (`int`, `uint`, `long`, etc.):** Performs a bitwise XOR. It compares the binary representations of the operands bit by bit, resulting in `1` where the corresponding bits differ, and `0` where they match.

```csharp theme={"dark"}
// Logical XOR
bool logicalResult1 = true ^ false; // Evaluates to true
bool logicalResult2 = true ^ true;  // Evaluates to false

// Bitwise XOR
int bitwiseResult = 5 ^ 3;          // Evaluates to 6
/* Bitwise evaluation:
   0101 (5 in binary)
 ^ 0011 (3 in binary)

   0110 (6 in binary)
*/
```

The binary `^` operator also supports compound assignment via `^=`, which applies the XOR operation and assigns the result to the left operand.

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

## 2. Index from End Operator

Introduced in C# 8.0, when used as a unary prefix operator, `^` instantiates a `System.Index` struct. It specifies an index relative to the end of a sequence (such as an array, `Span<T>`, or string), rather than the beginning.

The index `^n` translates to `sequence.Length - n` at runtime. Consequently, `^1` points to the last element in the sequence. `^0` points to the sequence length itself; while `^0` throws an `IndexOutOfRangeException` if used for direct element access, it is syntactically valid and required when defining an exclusive upper bound in a `System.Range`.

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

// Direct indexing
int last = numbers[^1];         // Evaluates to 50 (numbers.Length - 1)
int secondToLast = numbers[^2]; // Evaluates to 40 (numbers.Length - 2)

// Range indexing (..) using Index from End
// Extracts from index 1 up to, but not including, the last element
int[] slice = numbers[1..^1];   // Evaluates to { 20, 30, 40 }
```

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