Senior 3 min · March 06, 2026

Bit Manipulation Problems — Signed Shift Traps & XOR Tricks

Arithmetic shift on negative numbers caused infinite loops in production.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • XOR cancels pairs: A^A=0, A^0=A — finds the single unique element in O(n) time and O(1) space
  • n & (n-1) clears the rightmost set bit — used for counting bits and power-of-two checks
  • Isolate rightmost set bit with n & -n — foundational for Fenwick trees and bitmask grouping
  • Largest performance win: loops replaced by constant-time bitwise ops; up to 10x faster than arithmetic alternatives
  • Biggest production pitfall: operator precedence — n & 1 == 0 is parsed as n & (1 == 0); always parenthesize
  • Signed vs unsigned shifts: Java's >> preserves sign, >>> is logical; wrong choice breaks bit-counting on negative numbers
Plain-English First

Imagine every number is secretly a row of light switches — each switch is either ON (1) or OFF (0). Bit manipulation is just flipping, checking, or combining those switches directly, without doing normal math. It's like adjusting Christmas lights one bulb at a time instead of replacing the whole string. Computers think in exactly this language, so when you work at the switch level, you skip all the middleman work and get blazing-fast results.

Every major tech company — Google, Meta, Amazon — consistently includes at least one bit manipulation problem in their coding rounds. That's not a coincidence. These problems test whether you understand how a CPU actually works at its lowest level, separating candidates who memorized patterns from those who genuinely understand computation. A single XOR or a bitwise AND can replace entire loops, cut memory usage in half, and run in constant time — and interviewers know it.

The core challenge with bit manipulation isn't the operators themselves — you likely know & | ^ ~ << >>. The challenge is pattern recognition: seeing a problem about finding a missing number or detecting duplicates and immediately knowing 'this is an XOR problem.' Without that pattern library wired into your brain, you'll stare at the problem and reach for a HashSet when a two-line XOR solution was sitting right there.

By the end of this article you'll have five rock-solid bit manipulation patterns locked in with real runnable code, you'll understand exactly why each trick works at the binary level (not just that it works), and you'll know the specific gotchas that cause smart engineers to get these wrong under interview pressure — things like signed integer overflow, arithmetic vs. logical right shifts, and why Brian Kernighan's bit-counting trick is faster than a naive loop.

The Power of XOR: Solving 'Single Number' (LeetCode 136)

The XOR (exclusive OR) operator is the 'magic wand' of bit manipulation. Its properties are unique: any number XORed with itself is 0 ($A \oplus A = 0$), and any number XORed with 0 is itself ($A \oplus 0 = A$). It is also commutative and associative.

In an interview, if you see a problem where every element appears twice except for one, don't reach for a Map or a Set. That's $O(n)$ space. By XORing every number in the array together, all pairs cancel each other out, leaving only the unique element. This is the gold standard for efficiency: $O(n)$ time and $O(1)$ space.

io/thecodeforge/bit/XorUniqueFinder.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package io.thecodeforge.bit;

public class XorUniqueFinder {
    /**
     * Finds the element that appears only once in an array where
     * every other element appears exactly twice.
     */
    public int singleNumber(int[] nums) {
        int uniqueElement = 0;
        for (int currentNum : nums) {
            // XOR operation effectively 'cancels' out pairs
            uniqueElement ^= currentNum;
        }
        return uniqueElement;
    }

    public static void main(String[] args) {
        XorUniqueFinder finder = new XorUniqueFinder();
        int[] input = {4, 1, 2, 1, 2};
        System.out.println("The single number is: " + finder.singleNumber(input));
    }
}
Output
The single number is: 4
Forge Tip: XOR is order-independent
Because XOR is commutative, the numbers don't need to be adjacent. {1, 2, 1} and {1, 1, 2} both resolve to 2. This is why it works perfectly on unsorted arrays.
Production Insight
In production logging, XOR is used to aggregate IDs across services without storing every event.
The risk: if a duplicate appears an odd number of times (e.g., three copies), the cancellation fails — XOR counts parity, not exact pairs.
Rule: XOR-based deduplication works only when you guarantee every real pair appears exactly twice.
Key Takeaway
XOR cancels pairs.
Use it for parity checks, missing element detection, and memory-safe aggregations.
If you see "every element appears twice except one" — reach for XOR first.

Counting Set Bits: Brian Kernighan’s Algorithm (LeetCode 191)

Interviewers often ask for the 'Hamming Weight' or the number of set bits (1s) in an integer. A naive approach checks every bit one by one ($O(32)$). However, Brian Kernighan’s algorithm is much faster because it only iterates as many times as there are set bits.

The trick lies in the expression n & (n - 1). This operation always flips the least significant (rightmost) set bit to zero. By repeating this until the number becomes zero, you count the bits in record time.

io/thecodeforge/bit/BitCounter.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package io.thecodeforge.bit;

public class BitCounter {
    /**
     * Counts the number of set bits (1s) in an integer.
     * Efficiency: O(k) where k is the number of set bits.
     */
    public int countSetBits(int inputNumber) {
        int count = 0;
        while (inputNumber != 0) {
            // This clears the least significant set bit
            inputNumber &= (inputNumber - 1);
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        BitCounter counter = new BitCounter();
        // Binary for 7 is 0111 (3 bits)
        System.out.println("Set bits in 7: " + counter.countSetBits(7));
    }
}
Output
Set bits in 7: 3
Watch Out: The Power-of-Two Check
A direct application of this logic is checking if a number is a power of two. If (n & (n - 1)) == 0 (and $n > 0$), it's a power of two because powers of two have exactly one set bit.
Production Insight
In database indexing, the number of set bits in a page offset determines which page cache slot to use.
A bug here (arithmetic shift instead of logical) caused MySQL to allocate wrong buffer sizes under load.
Rule: always use >>> when shifting negative or unknown inputs in bit-counting loops.
Key Takeaway
n & (n - 1) clears the rightmost set bit — it's the core of efficient bit counting.
Count bits only for 1s, not for all 32 bits.
Power-of-two check is a simple extension: (n > 0) && ((n & (n - 1)) == 0).

Power of Two Check: Beyond the Obvious

A number is a power of two if it has exactly one bit set in its binary representation. The expression n & (n - 1) == 0 directly verifies that. But there's a subtlety: 0 also satisfies (0 & -1) == 0, so you must include the n > 0 guard.

Beyond the standard check, interviewers may ask variations: "Is n a power of 4?" — here you combine the power-of-two check with a bit mask that isolates the parity of the set bit position: (n & 0x55555555) != 0 for 32-bit integers.

io/thecodeforge/bit/PowerOfTwo.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package io.thecodeforge.bit;

public class PowerOfTwo {
    public boolean isPowerOfTwo(int n) {
        return n > 0 && (n & (n - 1)) == 0;
    }

    public boolean isPowerOfFour(int n) {
        // Must be power of two and the set bit at an even position (0-indexed)
        return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0;
    }

    public static void main(String[] args) {
        PowerOfTwo checker = new PowerOfTwo();
        System.out.println("Is 8 power of two? " + checker.isPowerOfTwo(8));
        System.out.println("Is 16 power of four? " + checker.isPowerOfFour(16));
    }
}
Output
Is 8 power of two? true
Is 16 power of four? true
Mental Model: A Single 1-Bit
  • 1000 (8) has one bit; 1010 (10) has two bits.
  • Subtracting 1 flips the trailing zeros to ones: 1000 - 1 = 0111.
  • AND of these two values always gives zero for powers of two.
  • For non-powers, at least one matching 1-bit remains after subtraction.
Production Insight
Memory pool allocators often reserve sizes as powers of two to simplify alignment calculations.
A power-of-two test ensures the allocation size is valid — if it fails, the allocator may waste memory or crash.
Rule: always check n > 0 before the bit trick; zero is not a power of two but passes the mask.
Key Takeaway
Power-of-two check: (n > 0) && ((n & (n - 1)) == 0).
For power of four, add a mask for even-position bits: (n & 0x55555555) != 0.
If you see a problem about "powers of two," the interviewer expects this trick.

Isolating the Rightmost Set Bit: n & -n

The expression n & -n isolates the lowest set bit of an integer. How does it work? Two's complement negation inverts all bits and adds one. So -n = ~n + 1. ANDing with n zeroes out everything except the rightmost 1-bit. This is crucial for Fenwick trees (Binary Indexed Trees) and for grouping elements based on the last differing bit.

Example: n = 12 (1100), -n = -12 (0100 in two's complement, limited to lower bits). n & -n = 0100 = 4, which is the value of the lowest set bit.

io/thecodeforge/bit/LowestSetBit.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package io.thecodeforge.bit;

public class LowestSetBit {
    public int isolateLowestSetBit(int n) {
        if (n == 0) throw new IllegalArgumentException("No set bit in zero");
        return n & -n;
    }

    public static void main(String[] args) {
        LowestSetBit bitUtil = new LowestSetBit();
        System.out.println("Lowest set bit of 12 (1100) is: " + bitUtil.isolateLowestSetBit(12));
        System.out.println("Lowest set bit of -8 is: " + bitUtil.isolateLowestSetBit(-8));
    }
}
Output
Lowest set bit of 12 (1100) is: 4
Lowest set bit of -8 is: 8
Remember: n & -n only works for integers
The two's complement property holds for all ints except MIN_VALUE (0x80000000). For MIN_VALUE, -MIN_VALUE == MIN_VALUE due to overflow, so the technique returns MIN_VALUE itself (which is correct, the lowest set bit is the sign bit).
Production Insight
In Fenwick tree updates, you traverse indices by repeatedly adding the lowest set bit: i += i & -i.
A production bug: using i & -i on a signed integer with value 0 leads to division by zero or infinite loop.
Rule: always guard against zero before using this technique in index calculations.
Key Takeaway
Isolate lowest set bit: n & -n.
It's the foundation of Fenwick trees and bitmask iteration.
Test with zero and Integer.MIN_VALUE — those are edge cases.

Bitmasking: Enumerate All Subsets in O(2^n)

For problems like "generate all subsets" (LeetCode 78), a bitmask represents which elements are included. Each integer from 0 to (1 << n) - 1 maps to a unique subset. The j-th bit being 1 means the j-th element is in the subset. This is an $O(2^n * n)$ approach (or $O(2^n)$ if you process each subset directly).

The bitmask method is impressively compact: one loop over mask and inner loop over bits. Interviewers love it because it shows you understand binary representation and space efficiency — no recursion overhead.

io/thecodeforge/bit/SubsetEnumerator.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package io.thecodeforge.bit;

import java.util.ArrayList;
import java.util.List;

public class SubsetEnumerator {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        int n = nums.length;
        int totalSubsets = 1 << n; // 2^n
        for (int mask = 0; mask < totalSubsets; mask++) {
            List<Integer> subset = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if ((mask & (1 << i)) != 0) {
                    subset.add(nums[i]);
                }
            }
            result.add(subset);
        }
        return result;
    }

    public static void main(String[] args) {
        SubsetEnumerator enumerator = new SubsetEnumerator();
        int[] input = {1, 2, 3};
        System.out.println(enumerator.subsets(input));
    }
}
Output
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
Integer overflow with 1 << n
If n >= 31, 1 << n overflows the int sign bit. Use 1L << n and cast to int if needed, or use BigInteger for n > 62. Always check n against 30 before using int masks.
Production Insight
Bitmasking is used in permission systems: a single 64-bit long can represent up to 64 binary permissions.
A bug: using 1 << n where n > 31 on a 32-bit system leads to overflow and incorrect permission checks.
Rule: for production permission masks, always use long (64-bit) and shift with 1L.
Key Takeaway
Bitmasks enumerate subsets in 2^n iterations — no recursion overhead.
Watch out for 1 << n overflow when n >= 31.
Permission checks and configuration flags often use bitmasking for compact storage.
● Production incidentPOST-MORTEMseverity: high

Lost Log Entries from a Signed Shift in a Bit Counter

Symptom
Production logs stopped appearing after exactly 32 entries. CPU on the logging service spiked to 100% on a single core. Restarting the service temporarily fixed it.
Assumption
The developer assumed Java's >> operator would work for unsigned integers, which it does for small positives, but severity flags sometimes encoded bit 31 (the sign bit), making the number negative.
Root cause
The bit-counting loop used n >>= 1 to shift right. For negative numbers, the arithmetic shift preserves the sign bit, so n never becomes zero — infinite loop. A naive loop with >>> would have terminated correctly.
Fix
Replaced n >>= 1 with n >>>= 1 (logical shift) in the bit-counting function. Added a unit test with a negative input to catch regressions.
Key lesson
  • Always use >>> (logical right shift) when iterating over bits in Java unless you explicitly need sign extension.
  • Test bit-manipulation code with edge cases: negative numbers, zero, and the largest positive value (Integer.MAX_VALUE).
  • Code reviews for performance-critical bit logic should include a partner who understands signedness.
Production debug guideSymptom → Action guide for common bit-related failures4 entries
Symptom · 01
Bit-counting function returns negative count or never terminates
Fix
Check for arithmetic shift >> instead of logical shift >>>. Verify loop condition on negative inputs.
Symptom · 02
Power-of-two check incorrectly returns true for zero
Fix
Ensure the check includes n > 0 && (n & (n - 1)) == 0. Zero has no set bits but isn't a power of two.
Symptom · 03
Bitmask extraction yields unexpected bits on 64-bit machines
Fix
Explicitly use 1L << position to avoid integer overflow. In Java, 1 << 31 is negative; use long for masks beyond 31 bits.
Symptom · 04
XOR-based duplicate detection misses elements
Fix
Verify the operator: XOR is ^, not exponentiation. Also confirm the array length matches the expected pattern (all pairs + one singleton).
★ Quick Debug Cheat Sheet for Bit Manipulation ProblemsUse these commands and checks when you're stuck in an interview or debugging a production bit-manipulation bug.
Wrong answer for 'find the single number'
Immediate action
Print the XOR of all numbers on paper to verify cancellations.
Commands
int xor = 0; for (int n : nums) xor ^= n;
System.out.println(Integer.toBinaryString(xor));
Fix now
If output is not the expected unique number, check that every pair is exactly two occurrences (not more).
Bit count is too high or loops forever+
Immediate action
Print n in binary before and after each shift.
Commands
System.out.println(Integer.toBinaryString(n));
n >>>= 1; // or n >>= 1 ?
Fix now
Replace >> with >>> for unsigned shift. For negative n, >> never reaches zero.
Power-of-two check fails for large numbers+
Immediate action
Check if n <= 0. Then verify the expression.
Commands
boolean isPowerOfTwo = (n > 0) && ((n & (n - 1)) == 0);
System.out.println(isPowerOfTwo + " for n=" + n);
Fix now
Add parentheses around the bitwise operation: (n & (n - 1)) — operator precedence matters.
Subset enumeration yields duplicates or misses subsets+
Immediate action
Print the integer mask in binary and decode each bit position.
Commands
for (int mask = 0; mask < (1 << n); mask++) { ... }
System.out.println(Integer.toBinaryString(mask));
Fix now
If n > 31, use 1L << n (long) to avoid sign bit overflow. For n > 63, BigInteger is needed.
Bit Manipulation Operations Quick Reference
OperationBitwise SyntaxCommon Interview Use Case
Check if Even/Odd(n & 1) == 0Faster than modulo operator (%)
Clear rightmost set bitn & (n - 1)Counting bits or Power-of-Two check
Isolate rightmost set bitn & -nUsed in Fenwick Trees or advanced grouping
Swap two numbersa ^= b; b ^= a; a ^= b;Classic 'no-extra-variable' swap trick
Check i-th bit(n >> i) & 1Building bitmasks or flags
Set i-th bitn | (1 << i)Permission flags or status tracking
Clear i-th bitn & ~(1 << i)Disabling a specific feature flag
Toggle i-th bitn ^ (1 << i)Alternating state machines

Key takeaways

1
XOR is your best friend for 'finding the odd one out' because $A \oplus A = 0$. Use it to achieve $O(1)$ space complexity.
2
The expression n & (n - 1) is a powerhouse—it flips the rightmost 1-bit to 0. It is the core of Brian Kernighan’s counting algorithm and power-of-two tests.
3
Bitwise operations are $O(1)$ at the CPU level, making them significantly faster than arithmetic operations in performance-critical code.
4
Always clarify with the interviewer if the integer is signed or unsigned, as this dictates whether you use the >>> or >> operator.
5
Isolate the rightmost set bit with n & -n
indispensable for Fenwick trees and bitmask iteration.
6
When enumerating subsets with bitmasks, watch out for overflow of 1 << n when n >= 31. Use 1L << n for large sets.
7
In production, always parenthesize bitwise expressions to avoid precedence pitfalls. Unit test with zeros, negatives, and MAX/MIN values.

Common mistakes to avoid

4 patterns
×

Forgetting operator precedence

Symptom
Expression n & 1 == 0 returns boolean but then bitwise ANDs with n, causing unexpected results. In Java, == has higher precedence than &.
Fix
Always use parentheses around bitwise operations: (n & 1) == 0. Also check similar issues with shift operators: (n >> i) & 1 is safe but n & 1 << i needs parentheses: n & (1 << i).
×

Ignoring signedness when right-shifting

Symptom
Bit-counting on negative numbers loops forever or gives wrong count. Java's >> is arithmetic (preserves sign), >>> is logical (always fills with zero).
Fix
Use >>> for shifting bits in count loops. If you need to treat an int as unsigned, use Integer.parseUnsignedInt or work with >>> throughout.
×

Integer overflow with large left shifts

Symptom
1 << 31 produces -2147483648 because the bit shifts into the sign bit. 1 << 32 on a 32-bit int is undefined (actually shift distance mod 32).
Fix
Use 1L << position for positions beyond 30. If you need 64 bits, work with long. For arbitrary shifts, consider BigInteger or handle overflow explicitly.
×

Assuming XOR solves all 'appears twice' variants

Symptom
In problems where one number appears once and all others appear three times, simple XOR fails. XOR counts parity, not exact multiplicity.
Fix
For 'appears exactly three times except one', use counting of each bit modulo 3 or a state machine (three-state solution). Don't default to XOR without checking pair count.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01JUNIOR
LeetCode 136: Single Number — Given a non-empty array of integers, every...
Q02SENIOR
LeetCode 190: Reverse Bits — Reverse bits of a given 32-bit unsigned int...
Q03SENIOR
LeetCode 338: Counting Bits — Given an integer n, return an array of len...
Q04SENIOR
LeetCode 201: Bitwise AND of Numbers Range — Given a range [m, n], find ...
Q05SENIOR
LeetCode 137: Single Number II — Given an integer array where every elem...
Q01 of 05JUNIOR

LeetCode 136: Single Number — Given a non-empty array of integers, every element appears twice except for one. Find that single one in O(1) space.

ANSWER
Use XOR. Initialize result = 0. For each number n, result ^= n. All pairs cancel out, leaving the unique element. Time: O(n), space: O(1).
FAQ · 5 QUESTIONS

Frequently Asked Questions

01
Why use bit manipulation instead of normal arithmetic?
02
What is the difference between >> and >>> in Java?
03
Is bit manipulation still relevant in modern high-level languages?
04
How do I avoid overflow when shifting 1 by n positions?
05
Can I use bit manipulation for negative numbers safely?
🔥

That's Coding Patterns. Mark it forged?

3 min read · try the examples if you haven't

Previous
Two Pointer Interview Problems
9 / 17 · Coding Patterns
Next
Stack and Queue Interview Problems