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

# Rust Bitwise XOR

The `^` operator in Rust performs a bitwise Exclusive OR (XOR) operation on integer types and a logical XOR operation on boolean types. It evaluates the operands bit-by-bit, returning `1` (or `true`) if the corresponding bits differ, and `0` (or `false`) if they are identical.

## Evaluation Logic (Truth Table)

For any given bit position in the operands, the `^` operator applies the following logic:

|   Operand A   |   Operand B   | Result (A ^ B) |
| :-----------: | :-----------: | :------------: |
| `0` / `false` | `0` / `false` |  `0` / `false` |
| `0` / `false` |  `1` / `true` |  `1` / `true`  |
|  `1` / `true` | `0` / `false` |  `1` / `true`  |
|  `1` / `true` |  `1` / `true` |  `0` / `false` |

## Integer Bitwise XOR

When applied to integers (`u8`, `i32`, `usize`, etc.), Rust aligns the binary representations of both operands and applies the XOR logic to each corresponding bit. Both operands must be of the exact same type.

```rust theme={"dark"}
let a: u8 = 0b1010_1100;
let b: u8 = 0b0110_0101;

let result = a ^ b;

// Evaluation breakdown:
//   1010_1100 (a)
// ^ 0110_0101 (b)
// --------
//   1100_1001 (result)
```

## Boolean Logical XOR

When applied to `bool` types, `^` acts as a strict logical inequality operator. It returns `true` only if exactly one of the operands is `true`. Unlike `||` or `&&`, the `^` operator does not short-circuit; it always evaluates both operands.

```rust theme={"dark"}
let val1 = true ^ false; // true
let val2 = true ^ true;  // false
let val3 = false ^ false; // false
```

## Trait Implementations

Under the hood, the `^` operator is syntactic sugar for the `std::ops::BitXor` trait.

```rust theme={"dark"}
pub trait BitXor<Rhs = Self> {
    type Output;
    fn bitxor(self, rhs: Rhs) -> Self::Output;
}
```

Custom types can support the `^` operator by implementing this trait.

```rust theme={"dark"}
use std::ops::BitXor;

struct Flags(u8);

impl BitXor for Flags {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        Flags(self.0 ^ rhs.0)
    }
}
```

## Compound Assignment (`^=`)

Rust also provides the `^=` operator, which performs the XOR operation and assigns the result back to the left-hand operand in a single step. This requires the left-hand variable to be mutable and relies on the `std::ops::BitXorAssign` trait.

```rust theme={"dark"}
let mut x: u8 = 0b1111_0000;
x ^= 0b1010_1010;

// x is now 0b0101_1010
```

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