Green binary code matrix representing bit manipulation

Bit Manipulation for Coding Interviews: 2026 Guide

Bit manipulation is one of the most powerful yet underrated skills in coding interviews. While candidates spend weeks practicing dynamic programming and graph algorithms, bit manipulation questions can catch even experienced developers off guard. Mastering a handful of bit manipulation tricks lets you solve problems in O(1) space and often in fewer lines of code than brute-force approaches.

In this guide, you will learn the essential bitwise operations, core patterns, and interview-ready problems that top companies like Google, Amazon, and Microsoft love to ask in 2026.

Lines of programming code on a dark screen

Why Bit Manipulation Matters in Interviews

Bitwise operations run directly on the processor and execute in a single clock cycle. When an interviewer sees you reach for bit tricks instead of hash maps or extra arrays, it signals deep computer science fundamentals. Bit manipulation questions test whether you understand how data is represented at the hardware level, and whether you can think creatively about optimization.

Common interview scenarios include toggling flags, checking powers of two, counting set bits, finding missing or duplicate numbers, and encoding state into a single integer. These come up in system design rounds too, especially when discussing bloom filters, bitmask-based permissions, or compact data structures.

Essential Bitwise Operators You Must Know

Before diving into patterns, make sure you are fluent in the six core operators:

AND (&), OR (|), XOR (^)

The AND operator returns 1 only when both bits are 1. It is commonly used to mask bits or check if a specific bit is set. The OR operator returns 1 when at least one bit is 1, making it ideal for setting bits. The XOR operator returns 1 when exactly one bit is 1. XOR is arguably the most important operator in interviews because of its unique property: a ^ a = 0 and a ^ 0 = a.

NOT (~), Left Shift (<<), Right Shift (>>)

The NOT operator flips every bit. Left shift multiplies by powers of two, while right shift divides. Shifts are invaluable for generating masks and isolating specific bit positions. For example, 1 << k creates a mask with only the k-th bit set.

Developer writing code on a laptop screen

Core Bit Manipulation Patterns

Pattern 1: Check, Set, Clear, and Toggle a Bit

These four operations form the foundation. To check if the k-th bit is set: (n >> k) & 1. To set it: n | (1 << k). To clear it: n & ~(1 << k). To toggle it: n ^ (1 << k). Memorize these four formulas — they appear in nearly every bit manipulation problem.

Pattern 2: The XOR Trick for Finding Singles

The classic problem: given an array where every element appears twice except one, find the unique element. XOR all elements together. Since a ^ a = 0, all pairs cancel, leaving only the single element. This runs in O(n) time and O(1) space. The same principle extends to finding two unique elements by partitioning on a differing bit.

public int singleNumber(int[] nums) {
    int result = 0;
    for (int num : nums) {
        result ^= num;
    }
    return result;
}

Pattern 3: Power of Two Check

A number is a power of two if and only if it has exactly one bit set. The elegant check: n > 0 && (n & (n - 1)) == 0. The expression n & (n - 1) clears the lowest set bit. If the result is zero, there was only one bit set. This same trick also counts set bits: keep clearing the lowest bit until the number becomes zero.

Pattern 4: Brian Kernighan’s Bit Counting

To count the number of 1-bits in an integer, repeatedly apply n = n & (n - 1) and increment a counter each time. This runs in O(k) where k is the number of set bits, which is more efficient than checking all 32 bit positions.

public int hammingWeight(int n) {
    int count = 0;
    while (n != 0) {
        n = n & (n - 1);
        count++;
    }
    return count;
}

Pattern 5: Bitmask for Subsets

When generating all subsets of a set with n elements, iterate from 0 to 2^n – 1. Each integer acts as a bitmask where the j-th bit indicates whether the j-th element is included. This is the backbone of bitmask dynamic programming, a technique that appears in problems like the Traveling Salesman and task assignment.

Top Interview Problems to Practice

Here are the must-solve problems organized by difficulty:

Easy Level

Number of 1 Bits uses the Brian Kernighan pattern to count set bits. Power of Two applies the n & (n-1) trick. Reverse Bits tests your ability to isolate and reposition individual bits using shifts and masks. Missing Number combines XOR with the index trick: XOR all array values with all indices from 0 to n.

Medium Level

Single Number II extends the XOR pattern to elements appearing three times. You need to track bit counts modulo 3 using two state variables. Bitwise AND of Numbers Range requires finding the common prefix of two binary numbers, which you do by right-shifting both until they match. Counting Bits asks for the number of set bits for every number from 0 to n, solvable in O(n) using the DP relation dp[i] = dp[i & (i-1)] + 1.

Hard Level

Maximum XOR of Two Numbers uses a trie-based approach where you greedily choose bits from the most significant position. Minimum Number of Operations to Make Array Equal to Target involves bitwise difference analysis. These problems combine bit manipulation with other data structures and are favorites at FAANG companies.

Laptop with code editor open on desk

Common Pitfalls to Avoid

Signed vs. unsigned integers trip up many candidates. In Java, the right shift operator >> preserves the sign bit, while >>> fills with zeros. Use the unsigned right shift when working with negative numbers or when treating integers as 32-bit patterns.

Operator precedence is another common mistake. Bitwise operators have lower precedence than comparison operators in most languages. The expression n & 1 == 0 is parsed as n & (1 == 0), not (n & 1) == 0. Always use parentheses with bitwise expressions.

Integer overflow can also occur when left-shifting. Shifting 1 << 31 in Java gives a negative number because Java uses signed 32-bit integers. Use 1L << k when working with shifts beyond 30 positions.

Study Strategy for Bit Manipulation

Start by memorizing the four basic operations: check, set, clear, and toggle. Then solve the five easy-level problems listed above. Once those feel natural, work through the medium problems focusing on the XOR and n & (n-1) patterns. For most interviews, you will not need more than 10-15 problems to feel confident with bit manipulation.

Practice writing solutions without looking up formulas. The key insight is that most bit problems reduce to one of the core patterns: XOR cancellation, lowest-bit isolation, or bitmask enumeration. Once you recognize which pattern applies, the solution usually follows in under 10 lines of code.

Ready to take your interview preparation further? Explore more coding patterns and career resources at Niraswa AI to build a structured study plan that covers every topic top tech companies test.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *