Skip to main content
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.

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

Execution Example

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.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More