Senior 7 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 & Principal Engineer

20+ years shipping production code across the stack, with years spent interviewing engineers. Lessons pulled from things that broke in production.

Follow
Production
production tested
May 23, 2026
last updated
1,554
articles · all by Naren
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
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
✦ Definition~90s read
What is Bit Manipulation Interview Problems?

Bit manipulation problems test whether you can reason about computation at the hardware level — where every operation is a gate, every integer is a register, and performance isn't measured in milliseconds but in CPU cycles. These problems force you to think in binary: shifting bits left and right, masking with AND/OR/XOR, and exploiting two's complement arithmetic.

Imagine every number is secretly a row of light switches — each switch is either ON (1) or OFF (0).

They're a litmus test for systems thinking because they separate engineers who memorize APIs from those who understand how data actually moves through silicon. In production, you'll reach for bit tricks when you need lock-free flags, compact state machines, or hash-free deduplication — think Redis bitmaps tracking billions of users, or Linux kernel wait queues using atomic bit operations.

The XOR trick for LeetCode 136's 'Single Number' is the canonical example: XOR every integer in an array, and duplicates cancel to zero, leaving only the unique element. This works because XOR is its own inverse (a ^ a = 0) and commutative — a property you exploit in RAID parity, CRC checksums, and swap-free variable exchange.

Brian Kernighan's algorithm for counting set bits (n & (n-1)) repeatedly clears the lowest set bit, running in O(number of 1-bits) rather than O(number of total bits). This matters when you're profiling hot paths in embedded firmware or database bloom filters where every instruction counts.

Beyond the basics, isolating the rightmost set bit with n & -n leverages two's complement: -n = ~n + 1, so the AND isolates the lowest 1-bit. This is the foundation for Fenwick trees (binary indexed trees) used in competitive programming and real-time analytics.

Bitmasking with (1 << n) - 1 lets you enumerate all subsets of a set in O(2^n) — impractical for large n, but essential for small combinatorial problems like feature toggles or permission systems. The signed right shift trap (>> on negative integers) is where production bugs hide: in C/C++/Java, >> on signed ints is arithmetic (sign-extending), not logical.

Always use >>> in Java or cast to unsigned in C when you need zero-fill. These aren't academic puzzles — they're the difference between a hash collision bug that takes down a service and a bitmask that runs in constant time.

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.

Why Bit Manipulation Problems Are a Litmus Test for Systems Thinking

Bit manipulation problems test your ability to reason about data at the hardware level — directly operating on individual bits using AND, OR, XOR, NOT, and shifts. Unlike arithmetic, which abstracts away the binary representation, bit manipulation forces you to control every 0 and 1. The core mechanic is that integers are stored as fixed-width binary strings (e.g., 32-bit for Java int), and operations like x & (x - 1) clear the lowest set bit in O(1) — a trick that underpins counting bits, checking powers of two, and subset enumeration.

Key properties that matter in practice: XOR is its own inverse (a ^ b ^ b = a), making it ideal for finding unique elements in pairs. Signed right shift (>>) preserves the sign bit, while unsigned (>>>) shifts in zeros — a trap that silently corrupts logic when you assume zero-fill. Left shift (<<) multiplies by powers of two but can overflow without warning. These operations are constant-time, branch-free, and often replace loops or conditionals, yielding massive speedups in hot paths.

Use bit manipulation when you need extreme performance, memory efficiency (e.g., storing 32 flags in one int), or lock-free algorithms like concurrent counters with CAS. It’s not for readability — reserve it for tight loops, embedded systems, or interview problems that demand low-level reasoning. In production, a single wrong shift type can corrupt a hash function or break a network protocol, which is why understanding the mechanics separates senior engineers from cargo-cult coders.

Signed vs. Unsigned Shift
In Java, >> preserves the sign bit; >>> fills with zero. Using >> on a negative number when you expect zero-fill is a common source of infinite loops and wrong results.
Production Insight
A team used >> to extract bits from a 32-bit timestamp in a distributed lock — negative timestamps caused the lock to never release, bringing down the service.
Symptom: intermittent deadlocks that only reproduced under load, with no clear stack trace.
Rule: always use >>> for bit extraction unless you explicitly need sign extension; document the choice.
Key Takeaway
XOR is the Swiss Army knife: it toggles bits, finds missing numbers, and swaps values without a temp variable.
Signed right shift (>>) is a trap — prefer >>> for bit manipulation unless sign extension is intentional.
Bit operations are O(1) and branch-free; use them to replace loops in performance-critical paths, but never at the cost of clarity in shared code.
Bit Manipulation: Signed Shift Traps & XOR Tricks THECODEFORGE.IO Bit Manipulation: Signed Shift Traps & XOR Tricks Key techniques: XOR, Kernighan, power-of-two, bitmasking XOR: Single Number a ^ a = 0, a ^ 0 = a; find unique element Brian Kernighan's Algorithm n & (n-1) clears lowest set bit; count bits Power of Two Check n > 0 && (n & (n-1)) == 0 Isolate Rightmost Set Bit n & -n isolates lowest 1-bit Bitmasking Subsets Enumerate all subsets in O(2^n) ⚠ Signed right shift (>>) on negative numbers Use >>> for unsigned shift in Java/JS THECODEFORGE.IO
thecodeforge.io
Bit Manipulation: Signed Shift Traps & XOR Tricks
Bit Manipulation Interview Problems

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.

Easy Problems: The Ground Truth You Can't Afford to Skip

Competitors list easy problems like a grocery list. Here's the reality: these aren't 'warm-ups'. They're the foundation of every interview trick you'll face. When an interviewer asks 'Check if K-th bit set,' they're testing if you understand that bit positions start at 0, not 1 — a single off-by-one error kills your signal processing code in production.

The power of these basics: computing modulus by 2^n is just n & mask. No modulo hardware. No division. That's why your crypto library's hash function runs in constant time — no branches, no floating point. Opposite signs check isn't academic; it's how you detect overflow in a payment system without triggering undefined behavior.

Every senior engineer knows: the XOR trick to swap variables (a ^= b; b ^= a; a ^= b) saved my ass when a register file ran out in embedded C. These aren't puzzles. They're cost models for cycle count and cache misses.

check_kth_bit.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// io.thecodeforge — interview tutorial

def is_kth_bit_set(value: int, k: int) -> bool:
    """Checks if the k-th bit (0-indexed) of value is set to 1."""
    if k < 0 or k > 31:
        raise ValueError(f"k={k} out of range for 32-bit integer")
    return (value >> k) & 1 == 1

def set_kth_bit(value: int, k: int) -> int:
    """Sets the k-th bit to 1, returns new integer."""
    mask = 1 << k
    return value | mask

if __name__ == "__main__":
    num = 0b00000101  # 5
    print(f"Original: {bin(num)}")
    print(f"Bit 0 set? {is_kth_bit_set(num, 0)} (expected True)")
    print(f"Bit 1 set? {is_kth_bit_set(num, 1)} (expected False)")
    num_with_bit_1 = set_kth_bit(num, 1)
    print(f"After setting bit 1: {bin(num_with_bit_1)} (expected 0b111 = 7)")
Output
Original: 0b101
Bit 0 set? True (expected True)
Bit 1 set? False (expected False)
After setting bit 1: 0b111 (expected 0b111 = 7)
Senior Shortcut:
Always mask before shift: (num >> k) & 1. Right-shift of negative integers is implementation-defined in C/C++ (arithmetic vs logical). Python handles it, but your interview code might need portable C. Don't assume.
Key Takeaway
Every bit trick reduces to three operations: mask, shift, XOR. Master these, and you can derive 90% of interview solutions in 30 seconds.

Is Bit Manipulation Important During Interviews? Here's the Brutal Truth

Competitors dodge the question with soft platitudes. I won't. Yes, it's important — but not for the reason you think. Interviewers aren't checking if you memorized Brian Kernighan's algorithm. They're checking if you can reason about the cost of a single CPU instruction under tight constraints.

When a senior asks you to 'find the only set bit in a number,' they're not testing your ability to loop over 32 bits. They want to see if you reach for n & -n without hesitation — because in a real-time audio driver, a 32-iteration loop drops a sample every 20 microseconds. That's not noise. That's a crash.

Here's what the interviewers won't tell you: Bit manipulation questions filter for systems thinking. A candidate who knows XOR for 'Single Number' but can't explain why it works (property: XOR is self-inverse and associative) will fail the follow-up. The real test is whether you see that the same technique underlies RAID parity calculations and memcached's consistent hashing.

Don't study these problems to solve LeetCode. Study them to understand why your cloud storage's error correction works at line speed.

single_number_derivation.pyPYTHON
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
// io.thecodeforge — interview tutorial

from typing import List

def find_single(numbers: List[int]) -> int:
    """Returns the element that appears once; all others appear twice.
    
    Why XOR works:
    - a ^ a = 0 (self-cancellation)
    - a ^ 0 = a (identity)
    - XOR is commutative and associative
    
    So: (a ^ a) ^ (b ^ b) ^ c = 0 ^ 0 ^ c = c
    """
    result = 0
    for num in numbers:
        result ^= num
    return result

if __name__ == "__main__":
    signal = [4, 1, 2, 1, 2]
    single = find_single(signal)
    print(f"Input: {signal}")
    print(f"Single element: {single}")
    print(f"Proof: {4 ^ 1 ^ 2 ^ 1 ^ 2} == {3 ^ 4 ^ 5} // associative check")
Output
Input: [4, 1, 2, 1, 2]
Single element: 4
Proof: 4 == 4 // associative check
Production Trap:
XOR is great for finding odd-occurrence elements, but it fails when memory is tight — you lose the original order. In a real-time log aggregator, use a hash set and pay the memory cost. XOR optimizes cycle count, not readability.
Key Takeaway
Interviewers ask bit manipulation to test if you can trade off time, space, and correctness under hardware constraints. The answer is never just the code — it's the why behind the mask.

Convert Characters to Uppercase or Lowercase

Bit manipulation offers a clever, branchless trick: clear the 6th bit (bit 5, 0-indexed) to force uppercase, or set it to force lowercase. ASCII 'A' (65, binary 1000001) differs from 'a' (97, 1100001) by exactly 32 — bit 5 is 0 for uppercase, 1 for lowercase. Using ch & ~(1 << 5) clears that bit, mapping both 'a' and 'A' to 'A'. Using ch | (1 << 5) sets that bit, mapping both to 'a'. This works for all letters and is immune to off-by-one errors from arithmetic subtraction. It's especially useful in constrained embedded systems, parser loops, or any scenario where branching is expensive. The same bit 5 property also toggles case with ch ^ (1 << 5). Interviewers prize this because it demonstrates intimate ASCII knowledge and a systems-thinking mindset — you're manipulating the underlying binary representation, not relying on library functions.

ConvertCase.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// io.thecodeforge — interview tutorial
// Convert char to uppercase/lowercase using bit 5

CHAR = 'a'

# Uppercase: clear bit 5
upper = chr(ord(CHAR) & ~(1 << 5))
print(upper)  # 'A'

# Lowercase: set bit 5
lower = chr(ord(CHAR) | (1 << 5))
print(lower)  # 'a'

# Toggle case: XOR bit 5
toggled = chr(ord(CHAR) ^ (1 << 5))
print(toggled)  # 'A'
Output
A
a
A
Production Trap:
Only works for ASCII letters; fails on non-alphabetic characters or Unicode. For full UTF-8, use standard library functions.
Key Takeaway
Bit 5 is the case switch; clearing sets uppercase, setting sets lowercase.

What to Learn Next + Frequently Asked Questions

What to learn next: Master two's complement representation, bit shifts with signed vs unsigned integers, and practical XOR tricks (swap without temp variable). Then explore advanced topics: circular bit shifts, gray code generation, and SSE/AVX SIMD intrinsics. Finally, study bitboards in chess engines and CRC checksums — these appear in system design and embedded roles.

Frequently Asked Questions:

  1. Does bit manipulation replace arithmetic? Only for specific cases (power-of-two divisors). General arithmetic is safer.
  2. Is Python’s big integer a risk? Yes — Python handles arbitrary width, so n = 1 << 200 works, but shifting negative numbers has different semantics. Always mask to fixed width (e.g., & 0xFFFFFFFF) for simulation.
  3. Which problems guarantee an interview hit? Single Number (XOR), Counting Set Bits (Kernighan's), Power of Two, and Subset Enumeration — these appear at FAANG+ frequently.
  4. How do I handle endianness? Bitwise operations are endian-agnostic; only byte-level access (union, casting) is affected.
NextSteps.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
// io.thecodeforge — interview tutorial
// FAQ: swap without temp using XOR

a, b = 5, 3
a ^= b
b ^= a
a ^= b
print(a, b)  # (3, 5)

# Simulate 32-bit mask for safety
MASK = 0xFFFFFFFF
n = -1
print(n & MASK)  # 4294967295 as unsigned 32-bit
Output
3 5
4294967295
Systems Insight:
Interviewers test bit manipulation to see if you reason about hardware. Learn why x & (x-1) clears the lowest set bit — that's Kernighan's core.
Key Takeaway
Combine bit tricks with mask-and-shift discipline; always specify bit width for portability.
● 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?
N
Naren Founder & Principal Engineer

20+ years shipping production code across the stack, with years spent interviewing engineers. Lessons pulled from things that broke in production.

Follow
Verified
production tested
May 23, 2026
last updated
1,554
articles · all by Naren
🔥

That's Coding Patterns. Mark it forged?

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

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