Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Bitwise Tricks

JavaScript bitwise operators manipulate the binary representation of 32-bit integer operands. They include logical bit operations and left or right shifts, and are mainly useful for specialized low-level tasks.

How it works

JavaScript bitwise operators convert numeric operands to 32-bit integers and apply the operation to their binary representations. AND, OR, XOR, and NOT manipulate corresponding bits, while shift operators move bits left or right; the result is an ordinary JavaScript number.

Syntax

a & b; a | b; a ^ b; ~a; a << b; a >> b; a >>> b

Returns

Each operator produces a JavaScript number resulting from its bitwise operation.

Key points

  • Bitwise operators work on 32-bit integer representations rather than directly on decimal values.
  • The available operators are AND (&), OR (|), XOR (^), NOT (~), left shift (<<), right shift (>>), and zero-fill right shift (>>>).
  • The same family of bitwise operators is common across many programming languages.
  • Bitwise syntax and concepts are broadly shared, which can help when translating low-level algorithms between languages.
  • Bitwise operations can be appropriate in specialized areas such as cryptography.

Common mistakes

  • Operands are handled as 32-bit integers, so bitwise operations are unsuitable when preserving arbitrary-precision numeric values matters.

Example: Basic bitwise operations

const flags = 0b1010;
const read = 0b0010;

console.log((flags & read) !== 0); // true
console.log(flags ^ read);          // 8
console.log(flags << 1);            // 20

This runnable example checks a bit with AND, combines differences with XOR, and shifts bits left.

When to use it

Bit flags

Testing whether a flag bit is set in a compact bit mask.

Packed options

Combining or removing compact binary options.

Low-level algorithms

Implementing specialized low-level numeric or cryptographic algorithms.

  • operators
  • numbers
  • binary representation
  • AND (&)
  • OR (|)
  • XOR (^)
  • NOT (~)
  • LEFT SHIFT (<<)

Source: Basic operators, maths. Checked 2026-10-15.