How do bitwise operators like >>>, &, and | behave differently on BigInt versus Number?
Short answer
Most bitwise operators work on BigInt the same way conceptually (treating it as an arbitrary-precision two's-complement integer), EXCEPT the unsigned right shift (>>>), which is explicitly NOT supported on BigInt at all, since BigInt has no fixed bit-width to define an…
Advertisement
AModel answer
Most bitwise operators work on BigInt the same way conceptually (treating it as an arbitrary-precision two's-complement integer), EXCEPT the unsigned right shift (>>>), which is explicitly NOT supported on BigInt at all, since BigInt has no fixed bit-width to define an 'unsigned' representation against.
Example
5n & 3n; // 1n -- works fine 5n | 2n; // 7n -- works fine // (-5n) >>> 1n; // TypeError: BigInts have no unsigned right shift
Real-world example
Implementing custom arbitrary-precision bit-manipulation logic, like a big-integer hashing algorithm.
Likely follow-ups
- Why does regular Number bitwise math internally convert operands to 32-bit integers first?
Related topics