Bitwise Operators in JavaScript
JavaScript bitwise operators (&, |, ^, ~, <<, >>, >>>) coerce operands to 32-bit ints, operate, and return signed 32-bit results (except >>>, which returns unsigned). They're niche in typical app code but essential in parsers, hashes, color math, and flags — plus the famous | 0 floor trick for non-negative numbers.
Know the two shifts: >> preserves sign (arithmetic), >>> zero-fills (logical) — -8 >> 1 is -4 while -8 >>> 1 is 2147483644. Beyond 32 bits, operands silently truncate — BigInt has its own bitwise set for big-integer bit work.
32-bit coercion
Operands convert via ToInt32 first — 2^32 wraps, fractions truncate, huge numbers lose high bits.
>> vs >>>
Arithmetic >> keeps the sign bit; logical >>> zero-fills — the difference matters only for negatives.
Flags and masks
Same idioms as C: test (f & READ), set (f |= W), clear (f &= ~X) — permission and option bitfields.
| 0 floors positives
x | 0 truncates toward zero for in-range positives — terse, fast, and cryptic to the uninitiated (comment it).
console.log(5 & 3, 5 | 3, 5 ^ 3);console.log(1 << 3, -8 >> 1);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.