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

The `^=` (bitwise XOR assignment) operator performs a bitwise exclusive OR operation on the binary representations of its left and right operands, assigning the computed result back to the left operand.

```typescript theme={"dark"}
leftOperand ^= rightOperand;
```

While conceptually similar to the expanded assignment syntax (`leftOperand = leftOperand ^ rightOperand`), the `^=` operator evaluates the `leftOperand` exactly once. This is a critical distinction when the left operand contains expressions with side effects. For example, `arr[i++] ^= 2` increments `i` only once, whereas `arr[i++] = arr[i++] ^ 2` would evaluate the index and increment `i` twice.

## Execution Mechanics

When the `^=` operator is invoked, the TypeScript runtime (via JavaScript) executes the following sequence based on the operand types:

1. **Numeric Conversion & Truncation:**
   * For `number` operands: Both operands are evaluated and implicitly converted to 32-bit signed integers using two's complement representation. Any fractional components are truncated.
   * For `bigint` operands: Both operands are evaluated at arbitrary precision without being truncated to 32 bits.
2. **Bitwise Comparison:** The operator compares the operands bit by bit. It yields a `1` in a given bit position if the corresponding bits of the operands differ, and a `0` if they are identical.
   * `0 ^ 0 = 0`
   * `0 ^ 1 = 1`
   * `1 ^ 0 = 1`
   * `1 ^ 1 = 0`
3. **Assignment:** The newly constructed numeric value (either a 32-bit integer or a `bigint`) is assigned to the memory location of the `leftOperand`.

## Step-by-Step Evaluation

```typescript theme={"dark"}
let a: number = 5; 
let b: number = 3; 

a ^= b; 

console.log(a); // Output: 6
```

**Binary Breakdown:**

* `a` (5) is represented as `...0000 0101`
* `b` (3) is represented as `...0000 0011`

```text theme={"dark"}
  0000 0101  (5)
^ 0000 0011  (3)

  0000 0110  (6)
```

## TypeScript Compiler Rules

TypeScript enforces strict type checking on bitwise assignment operators to prevent unintended runtime coercions.

* **Valid Assignment Targets:** The left operand must be a valid assignment target (l-value). This includes standalone variables, object properties, and array elements.
* **Left Operand Type Constraints:** The left operand must resolve to type `number`, `bigint`, or `any`. In modern TypeScript (5.0+), an `enum` variable is **not** a valid left operand for `^=`. While the bitwise XOR operation itself accepts enums, it evaluates to a `number`. This resulting `number` cannot be implicitly assigned back to an `enum` type.
* **Operand Mixing Rules:**
  * `number` and `bigint` types are strictly incompatible and cannot be mixed in the same operation.
  * `enum` members are treated as numeric values and can be used as the *right* operand when the left operand is a `number`.
  * `any` bypasses all strict type matching constraints, allowing operations with any right operand (though standard JavaScript runtime coercion rules will apply).

```typescript theme={"dark"}
let num: number = 10;
num ^= 2; // Valid

let arr = [1, 2, 3];
let i = 0;
arr[i++] ^= 2; // Valid (array element as l-value, 'i' is incremented once)

enum Flags { Read = 1, Write = 2 }
let currentFlags: number = Flags.Read;
currentFlags ^= Flags.Write; // Valid (left operand is number, right is enum)

let bigNum: bigint = 10n;
bigNum ^= 2n; // Valid

let dynamicVal: any = "5";
dynamicVal ^= 2; // Valid in TS (bypasses checks), evaluates to 7 at runtime

// Compiler Error TS2365: Operator '^=' cannot be applied to types 'number' and 'bigint'.
num ^= 2n; 

// Compiler Error TS2322: Type 'number' is not assignable to type 'Flags'.
let strictEnum: Flags = Flags.Read;
strictEnum ^= Flags.Write; 

let str: string = "5";
// Compiler Error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
str ^= 2; 
```

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