bitwise operators (&, |, ^, ~)
In JavaScript, the bitwise operators (&, |, ^, ~) work on the individual bits of integers. AND, OR, and XOR combine the matching bits of two operands, NOT inverts every bit of one operand, and each returns a number or a bigint.
With number operands, each operator first converts its operands to 32-bit signed integers in two’s complement. The conversion drops the fractional part and every bit above the lowest 32, so 3.7 | 0 is 3. A bigint keeps all of its bits, and mixing a bigint with a number throws a TypeError.
JavaScript Bitwise Operators Syntax
The three binary operators sit between two operands, and ~ goes before its single operand:
<left> & <right>
<left> | <right>
<left> ^ <right>
~<operand>
The four operators give these results for the operands 0b1100 and 0b1010, which are 12 and 10:
| Operator | Name | Example | Result |
|---|---|---|---|
& |
Bitwise AND | 0b1100 & 0b1010 |
8 (0b1000) |
| |
Bitwise OR | 0b1100 | 0b1010 |
14 (0b1110) |
^ |
Bitwise XOR | 0b1100 ^ 0b1010 |
6 (0b0110) |
~ |
Bitwise NOT | ~0b1100 |
-13 |
For any 32-bit integer x, ~x equals -(x + 1), because NOT flips the sign bit too.
JavaScript Bitwise Operators Examples
Say an app stores a user’s permissions as bit flags in one integer. The | operator turns flags on, & tests and clears them, and ^ toggles one:
> const READ = 0b001;
undefined
> const WRITE = 0b010;
undefined
> const DELETE = 0b100;
undefined
> const permissions = READ | WRITE;
undefined
> (permissions & WRITE) !== 0
true
> (permissions & DELETE) !== 0
false
> permissions & ~WRITE
1
> permissions ^ DELETE
7
Clearing a flag pairs & with ~WRITE, a mask that has every bit set except the write bit. XOR turns the delete bit on here, and applying it a second time would turn it off again.
Say some code drops the fraction from a millisecond timestamp with | 0. The value needs more than 32 bits, so the result wraps around:
> const timestamp = 1794268800123.6;
undefined
> timestamp | 0
-1027529605
> Math.trunc(timestamp)
1794268800123
> 1794268800123n | 0n
1794268800123n
The Math.trunc() method handles any number, and bigint operands keep every bit.
JavaScript Bitwise Operators Gotchas
- The equality operators bind tighter than
&,|, and^, soflags & WRITE === 0comparesWRITE === 0first. Parentheses around the bitwise part fix it. - Unlike
&&and||, the&and|operators always evaluate both operands and never return an operand itself, sotrue & falseevaluates to the number0, notfalse. - The
x | 0and~~xidioms truncate correctly only between -2147483648 and 2147483647. Larger values lose their high bits.
JavaScript Bitwise Operators Use Cases
- Packing several Boolean flags into one integer
- Setting, testing, clearing, and toggling flags with masks
- Keeping the low bits of a value with a mask such as
& 0xff - Checking whether an integer is odd with
& 1 - Combining bytes into checksums and hashes with
^
Related Resources
Course
JavaScript Number Precision, BigInt, and Formatting for Pythonistas
Port a coffee roastery's month-end invoice script from Python to JavaScript, and learn to keep money and big IDs exact, catch Infinity and NaN, and format numbers like Python.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026