Bit Manipulation Interview Questions 2026

Bit manipulation appears in roughly 5-10% of coding interviews, concentrated at senior and staff levels. The trick is recognizing when to apply it and knowing the core XOR properties.

XOR Properties

XOR key properties: x ^ x = 0, x ^ 0 = x, XOR is commutative and associative. These properties enable elegant solutions: find the single non-duplicate in an array of paired numbers by XORing all elements.

Find two non-duplicate numbers in an array of paired numbers: XOR all elements to get a^b. Find any set bit in a^b. Partition array into two groups based on that bit. XOR each group separately to get a and b.

Bit Tricks

Check if n is a power of 2: n > 0 and (n & (n-1)) == 0. Clear the lowest set bit: n & (n-1). Get the lowest set bit: n & (-n). Count set bits (Brian Kernighan): loop n = n & (n-1) until n is 0, counting iterations.

Set bit k: n |= (1 << k). Clear bit k: n &= ~(1 << k). Toggle bit k: n ^= (1 << k). Check bit k: (n >> k) & 1. These are the building blocks for all bit manipulation.

Bitmask DP

Bitmask DP represents a subset of elements as a bitmask. State: dp[mask][last] = minimum cost to visit the set of nodes in mask, ending at last. Transition: extend the path to any unvisited node.

Applications: traveling salesman problem (exact for small n ≤ 20), assignment problems, covering problems. The state space is O(2^n * n); feasible for n ≤ 20 in competitive programming and some interview contexts.

Integer Overflow and Negative Numbers

Two's complement: negative numbers in binary. Left shift by 1 doubles; right shift by 1 halves (arithmetic shift preserves sign, logical shift does not). Overflow: INT_MAX + 1 wraps to INT_MIN in C/Java.

Reverse integer without overflow: check before each multiply/add: if result > INT_MAX // 10, overflow would occur. This check pattern applies to any arithmetic that might overflow.

Browse Real Bit Manipulation Questions

Actual bit problems from top tech company coding interviews.

Browse Bit Manipulation Questions