Skip to main content

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.

The &= (bitwise AND assignment) operator performs a bitwise AND operation between the binary representations of its left and right operands, assigning the computed 32-bit integer result back to the left operand.
x &= y;
// Equivalent to: x = x & y;

Mechanical Behavior

  1. Type Coercion (ToInt32): Before the operation occurs, the TypeScript/JavaScript runtime implicitly coerces both operands into 32-bit signed integers. Fractional values are truncated.
  2. Bitwise Evaluation: The operator compares the operands bit by bit. A bit in the resulting integer is set to 1 if and only if the corresponding bits in both operands are 1. If either bit is 0, the resulting bit is 0.
  3. Assignment: The newly calculated 32-bit integer is assigned to the memory location of the left operand.

Truth Table

Bit A (Left)Bit B (Right)Result (A & B)
111
100
010
000

Execution Example

let a: number = 14; // Binary: 00000000 00000000 00000000 00001110
let b: number = 9;  // Binary: 00000000 00000000 00000000 00001001

a &= b; 

// Bitwise Calculation:
//   00001110 (14)
// & 00001001 (9)
// -------
//   00001000 (8)

console.log(a); // Output: 8

TypeScript Compiler Constraints

While the underlying JavaScript engine will attempt to coerce any type into a number for bitwise operations, the TypeScript compiler enforces strict type safety:
  • Operand Types: TypeScript requires both the left and right operands to be of type number (or any). Attempting to use &= with strings, booleans, or objects will throw a compilation error (e.g., TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.).
  • Mutability: The left operand must be a mutable reference. It cannot be a constant (const) or a read-only property.
  • BigInt Support: The &= operator also supports bigint types, provided both operands are bigint. Mixing number and bigint will result in a TS2365 compiler error. When operating on bigint, the operation is not restricted to 32 bits.
Master TypeScript with Deep Grasping Methodology!Learn More