Bitwise operators
Bitwise operators work on the individual bits of a number. Try them in the calculator, then see the truth tables, examples and the mask tricks programmers use them for.
Truth tables
| A | B | A AND B | A OR B | A XOR B | NOT A |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 |
A bitwise operator applies its rule to each pair of bits in the same position, independently of the others.
The operators
| Operator | Symbol | What it does | Example |
|---|---|---|---|
| AND | & | 1 only where both bits are 1 | 1100 & 1010 = 1000 |
| OR | | | 1 where either bit is 1 | 1100 | 1010 = 1110 |
| XOR | ^ | 1 where the bits differ | 1100 ^ 1010 = 0110 |
| NOT | ~ | Flips every bit | ~1100 = 0011 (in 4 bits) |
| Left shift | << | Moves bits left, fills with 0; multiplies by 2 per place | 0011 << 2 = 1100 |
| Right shift | >> | Moves bits right; drops the bits that fall off | 1100 >> 2 = 0011 |
Masks: testing, setting, clearing and toggling bits
A mask is a number with 1s in the bit positions you care about. Combined with the right operator, it lets you work on single bits, for example the on/off flags packed into one byte. Here the flags start as 0101.
| Task | Code | Result |
|---|---|---|
| Test bit 2 | flags & 0b0100 | 0101 & 0100 = 0100 (not zero, so it’s set) |
| Set bit 1 | flags | 0b0010 | 0101 | 0010 = 0111 |
| Clear bit 0 | flags & ~0b0001 | 0101 & 1110 = 0100 |
| Toggle bit 3 | flags ^ 0b1000 | 0101 ^ 1000 = 1101 |
Shifts as multiplication and division
Shifting left by one place doubles a number, and by n places multiplies it by 2n, as long as no 1s fall off the top. Shifting right by one place halves it, rounding down. 13 << 1 is 26, and 13 >> 1 is 6.
Bitwise operators in JavaScript
- The operators
&,|and^first turn both numbers into 32-bit two’s complement integers, so anything beyond 32 bits is lost. >>is a sign-filling right shift: negative numbers stay negative.>>>fills with zeros instead.- The shift count is taken modulo 32, so
x << 33is the same asx << 1. - For wider numbers, BigInt values (such as
1n << 40n) work at any size. The calculator above uses them, which is why it can go past 64 bits.
Sources and method
- ECMA-262, sections 6.1.6.1.9 to 6.1.6.1.18 and 13.9: bitwise operators convert to 32-bit two’s complement, and shift counts are taken modulo 32. Checked 2026-11-25.
- ECMA-262, ECMAScript Language Specification, section 12.9.3 Numeric Literals: the 0b, 0o and 0x prefixes for binary, octal and hex. Checked 2026-11-25.
Results use exact whole-number arithmetic at any size. With a bit width chosen, the result is wrapped to that width the way a CPU register does, and overflow is checked separately for unsigned and signed (two’s complement) readings.
Questions
What is the difference between AND and OR?
AND gives 1 only where both bits are 1; OR gives 1 where at least one bit is 1.
What is XOR used for?
Toggling bits (XOR with 1 flips a bit), comparing values bit by bit (XOR is 0 where they match), and Gray code, which is a number XOR itself shifted right by one. The Gray code converter shows each XOR step.
Why does ~5 give −6?
NOT flips every bit, including the sign bit of a two’s complement number. ~x is always −x − 1.
Are bitwise and logical operators the same?
No. Logical operators such as && and || treat each whole value as true or false; bitwise operators work on every bit separately.