Loading lessons...
Bitwise and BigInt
Bitwise and BigInt
Two special areas of numbers: bitwise operators work on the binary bits of numbers, and BigInt handles numbers too big for regular Number.
Bitwise operators
Bitwise operators act on the 32-bit binary representation of numbers:
| Operator | Name | Example | ||
|---|---|---|---|---|
& | AND | 5 & 1 = 1 | ||
| ` | ` | OR | `5 | 1` = 5 |
~ | NOT | ~5 = -6 | ||
^ | XOR | 5 ^ 1 = 4 | ||
<< | left shift | 5 << 1 = 10 | ||
>> | right shift | 5 >> 1 = 2 |
Example: 5 is binary 101. 5 & 1 (101 & 001) = 001 = 1.
BigInt
BigInt lets you work with integers larger than the normal Number limit:
let big = 9007199254740991n;
let huge = 123456789012345678901234567890n;
Creating BigInt
- Add an
nto the end of an integer. - Or use the
BigInt()function:BigInt(10).
BigInt rules
- You cannot mix BigInt and regular Number in math without conversion.
- BigInt only works with whole numbers, no decimals.
TL;DR
- Bitwise operators work on binary bits: & | ~ ^ << >>.
- BigInt handles huge integers using an n suffix.
- BigInt cannot mix freely with regular numbers.