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

# TypeScript Bitwise XOR

The `^` operator in TypeScript is the bitwise XOR (exclusive OR) operator. It evaluates two numeric operands by performing a logical exclusive OR operation on each corresponding pair of bits. For standard `number` operands, this involves converting them to 32-bit signed integers, whereas `bigint` operands operate at arbitrary precision.

The operator evaluates bits according to the following truth table:

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

```typescript theme={"dark"}
const operandA: number = 0b1010;
const operandB: number = 0b1100;
const result: number = operandA ^ operandB; // 0b0110
```

## Evaluation Mechanics

When the TypeScript compiler transpiles and the JavaScript engine executes the `^` operator, the execution behavior depends strictly on the type of the operands:

**For `number` and numeric `enum` operands:**

1. **Type Coercion and Truncation:** Both operands are converted to 32-bit signed integers using two's complement representation. Any fractional components are discarded (e.g., `5.9` becomes `5`).
2. **Bitwise Comparison:** The engine aligns the 32 bits of both operands and compares them positionally.
3. **Return Value:** A new 32-bit signed integer is returned based on the XOR evaluation of the bit pairs.

```typescript theme={"dark"}
const a: number = 5;  // Binary: ...0000 0101
const b: number = 3;  // Binary: ...0000 0011

const result: number = a ^ b; 

// Bitwise Evaluation:
//   0101 (5 in decimal)
// ^ 0011 (3 in decimal)
// ---
//   0110 (6 in decimal)

console.log(result); // Outputs: 6
```

**For `bigint` operands:**
The operands are not truncated to 32 bits. The engine aligns the binary representation of the arbitrary-precision integers and performs the XOR operation across all bits, returning a new `bigint`.

## TypeScript Type Constraints

TypeScript enforces strict type checking on the `^` operator to prevent unintended runtime coercion:

**Number and Enum Operands:**
Operands can be of type `number`, `any`, or a numeric `enum`.

```typescript theme={"dark"}
enum Flags {
  Read = 1,
  Write = 2
}

const val1: number = 10;
const val2: Flags = Flags.Write;
const out: number = val1 ^ val2; // Valid, returns 8
```

**BigInt Operands:**
The `^` operator fully supports `bigint` types. Because `bigint` operates at arbitrary precision, no 32-bit truncation occurs.

```typescript theme={"dark"}
const big1: bigint = 10n;
const big2: bigint = 4n;
const bigOut: bigint = big1 ^ big2; // Valid, returns 14n
```

**Mixed Types (Compilation Error):**
TypeScript will throw a `ts(2365)` error if you attempt to mix `number` (or `enum`) and `bigint` operands, as the underlying JavaScript engine cannot implicitly mix these types for bitwise operations.

```typescript theme={"dark"}
const num: number = 5;
const big: bigint = 3n;

// Error: Operator '^' cannot be applied to types 'number' and 'bigint'.
const invalid = num ^ big; 
```

## Compound Assignment

TypeScript supports the compound assignment operator `^=`, which applies the bitwise XOR operation and assigns the resulting value back to the left operand.

```typescript theme={"dark"}
let x: number = 5;
x ^= 3; // Syntactic sugar for: x = x ^ 3;
// x is now 6
```

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