Computers store every number as bits: binary digits, 0 or 1. Bit manipulation means working with those bits directly, using special operators. It's fast and uses almost no memory, and a few tricks come up again and again in interviews.
Binary in one minute
In binary, each position is worth double the one to its right: 1, 2, 4, 8, 16, .... A number is the sum of the positions that hold a 1:
position value: 8 4 2 1
binary: 1 0 1 1
8 + 2 + 1 = 11- The rightmost bit is the lowest (bit 0). In JavaScript and in these problems, numbers usually have 32 bits (bits 0 to 31).
n & 1is the lowest bit:1ifnis odd,0if even.
The operators
They work on each bit position separately:
| Operator | Name | Result bit is 1 when... | Example (4 bits) |
|---|---|---|---|
a & b | AND | both bits are 1 | 1100 & 1010 = 1000 |
a | b | OR | at least one is 1 | 1100 | 1010 = 1110 |
a ^ b | XOR | the bits are different | 1100 ^ 1010 = 0110 |
~a | NOT | flips every bit | ~1100 = 0011 |
a << k | shift left | moves bits k places left (×2 each) | 0011 << 1 = 0110 |
a >> k | shift right | moves bits k places right (÷2 each), lowest bits fall off | 1011 >> 1 = 0101 |
Tricks worth knowing by heart
| Trick | What it does | Used in |
|---|---|---|
n & 1 | read the lowest bit (odd or even?) | Number of 1 Bits, Reverse Bits |
n >> 1 | drop the lowest bit (floor(n / 2)) | Counting Bits |
n & (n - 1) | turn off the lowest 1 bit | Number of 1 Bits |
n & (n - 1) === 0 | is n a power of two? | |
x ^ x = 0, x ^ 0 = x | a value cancels itself; order doesn't matter | Missing Number |
(n >> k) & 1 | read bit k | |
n | (1 << k) | turn bit k on | |
a ^ b and (a & b) << 1 | sum without carries, and the carries | Sum of Two Integers |
XOR: the pairing trick
Because x ^ x = 0 and the order of XORs doesn't matter, XOR-ing a list where every value appears twice gives 0. If one value appears only once, it's all that's left:
3 ^ 5 ^ 3 ^ 7 ^ 5 = (3 ^ 3) ^ (5 ^ 5) ^ 7 = 0 ^ 0 ^ 7 = 7Whenever things come in pairs and you need to find the odd one out, think XOR.
Negative numbers and two's complement
Computers store negative integers in two's complement: the top bit (bit 31) means "negative". For example, -1 is all 32 bits set to 1. You don't need the details for most problems, but you need to know the language traps:
- JavaScript:
&,|,^,<<,>>treat numbers as signed 32-bit. A result with bit 31 set looks negative. Use>>>(unsigned shift right) andx >>> 0to read a value as unsigned. - Python: integers have no fixed size, so there's no bit 31 to overflow into. When a problem needs 32-bit behavior, keep values inside 32 bits with
& 0xFFFFFFFF, and convert back to negative at the end.
How to recognize a bit manipulation problem
- The problem mentions binary, bits, 32-bit integers, or XOR.
- It asks you to avoid
+,-,*, or extra memory, in a way that seems impossible with normal arithmetic. - Values come in pairs except one.
- You need to represent a small set compactly (each bit = "is item
kin the set?").
Common mistakes
- Mixing up
&&/||(logical) with&/|(bitwise). - Forgetting operator precedence: in JavaScript,
n & 1 === 0meansn & (1 === 0). Write(n & 1) === 0. - Using
>>where you need>>>in JavaScript. - Looping
while (n > 0)whennmight be negative in JavaScript. Use!== 0.