JavaScript - Bitwise Operators
Overview
Estimated time: 20–30 minutes
Bitwise operators work on 32-bit integers. Use them for masks, flags, and low-level operations.
Learning Objectives
- Use AND, OR, XOR, NOT, shifts.
- Understand signed vs unsigned right shift.
Prerequisites
Examples
const READ=1<<0, WRITE=1<<1, EXECUTE=1<<2;
let perm = READ | WRITE; // 3
(perm & READ) !== 0; // true
perm = perm & ~WRITE; // remove WRITE
5 & 3 // 1
5 | 3 // 7
5 ^ 3 // 6
~5 // -6
5 << 1 // 10
5 >> 1 // 2
-5 >>> 1 // large positive (zero-fill)
Common Pitfalls
- All bitwise ops coerce to 32-bit signed integers (except >>> result is unsigned).