Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

Bit Manipulation

Working on the bits themselves: XOR, masks and shifts.

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:

text
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 & 1 is the lowest bit: 1 if n is odd, 0 if even.

The operators

They work on each bit position separately:

OperatorNameResult bit is 1 when...Example (4 bits)
a & bANDboth bits are 11100 & 1010 = 1000
a | bORat least one is 11100 | 1010 = 1110
a ^ bXORthe bits are different1100 ^ 1010 = 0110
~aNOTflips every bit~1100 = 0011
a << kshift leftmoves bits k places left (×2 each)0011 << 1 = 0110
a >> kshift rightmoves bits k places right (÷2 each), lowest bits fall off1011 >> 1 = 0101

Tricks worth knowing by heart

TrickWhat it doesUsed in
n & 1read the lowest bit (odd or even?)Number of 1 Bits, Reverse Bits
n >> 1drop the lowest bit (floor(n / 2))Counting Bits
n & (n - 1)turn off the lowest 1 bitNumber of 1 Bits
n & (n - 1) === 0is n a power of two?
x ^ x = 0, x ^ 0 = xa value cancels itself; order doesn't matterMissing Number
(n >> k) & 1read bit k
n | (1 << k)turn bit k on
a ^ b and (a & b) << 1sum without carries, and the carriesSum 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:

text
3 ^ 5 ^ 3 ^ 7 ^ 5  =  (3 ^ 3) ^ (5 ^ 5) ^ 7  =  0 ^ 0 ^ 7  =  7

Whenever 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) and x >>> 0 to 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 k in the set?").

Common mistakes

  • Mixing up && / || (logical) with & / | (bitwise).
  • Forgetting operator precedence: in JavaScript, n & 1 === 0 means n & (1 === 0). Write (n & 1) === 0.
  • Using >> where you need >>> in JavaScript.
  • Looping while (n > 0) when n might be negative in JavaScript. Use !== 0.

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.