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

# Java Bitwise XOR

The `^` operator in Java is the **Exclusive OR (XOR)** operator. It functions as a bitwise operator when applied to integer primitives and as a logical operator when applied to boolean primitives. It evaluates to `1` (or `true`) if and only if the operands are strictly different, and `0` (or `false`) if they are identical.

## Bitwise XOR (Integer Types)

When applied to integral data types (`byte`, `short`, `int`, `long`, `char`), the `^` operator performs a bit-by-bit comparison of the binary representations of the operands.

**Bitwise Truth Table:**

* `0 ^ 0` yields `0`
* `0 ^ 1` yields `1`
* `1 ^ 0` yields `1`
* `1 ^ 1` yields `0`

**Syntax Visualization:**

```java theme={"dark"}
int a = 5;      // Binary: 0000 0101
int b = 3;      // Binary: 0000 0011

int result = a ^ b; 
// Bitwise evaluation:
//   0000 0101
// ^ 0000 0011
// --------
//   0000 0110  -> Decimal: 6

System.out.println(result); // Outputs: 6
```

*Note: If operands are of different sizes (e.g., `int` and `long`), Java performs binary numeric promotion, widening the smaller type to match the larger type before applying the XOR operation.*

## Logical XOR (Boolean Types)

When applied to `boolean` operands, the `^` operator evaluates whether the two boolean expressions have different truth values.

Crucially, unlike the conditional logical operators (`&&` and `||`), the logical `^` operator **does not short-circuit**. Both the left-hand and right-hand operands are strictly evaluated before the XOR operation is applied.

**Logical Truth Table:**

* `false ^ false` yields `false`
* `false ^ true` yields `true`
* `true ^ false` yields `true`
* `true ^ true` yields `false`

**Syntax Visualization:**

```java theme={"dark"}
boolean x = true;
boolean y = false;

boolean result1 = x ^ y;     // Evaluates to true
boolean result2 = x ^ true;  // Evaluates to false

// Both methodA() and methodB() will execute because ^ does not short-circuit
boolean result3 = methodA() ^ methodB(); 
```

## Compound Assignment (`^=`)

Java provides a compound assignment operator for XOR, which applies the operation and assigns the result to the left-hand operand. It includes an implicit cast to the type of the left-hand operand.

**Syntax Visualization:**

```java theme={"dark"}
int val = 10;   // Binary: 1010
val ^= 4;       // Binary: 0100
// val is now 14 (Binary: 1110)

boolean flag = true;
flag ^= true;   
// flag is now false
```

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