Bit Manipulation Problems for Coding Interviews

Bit manipulation uses the binary form of numbers for compact state and fast tricks: XOR to find the odd one out, masks for subsets, shifts for powers of two and counting set bits. Many problems that look like maths reduce to a bitwise identity.

Know the operators and their precedence, how negative numbers are stored and how to test, set and clear a bit. Practise single number, counting bits, power of two and subset enumeration with a bitmask.

Coding problems

Related

Frequently asked questions

How do I get good at bit manipulation problems?

Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.

Can I run bit manipulation problems online here?

Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.