ReviseAlgo Logo

Bit Manipulation

Common Interview Patterns

Master bitwise patterns: XOR Single Number, Brian Kernighan Bit Counting, and Bitmasking.

Last Updated: August 2, 2026 20 min read

1. Introduction

What are BIT-MANIPULATION Interview Patterns?

BIT-MANIPULATION Interview Patterns represent the high-yield structural techniques used in technical interviews to solve linear, matrix, or non-linear computational problems efficiently.

Why study them?

Instead of memorizing individual LeetCode solutions, mastering these core patterns allows you to instantly recognize problem invariants and apply verified O(N) or O(N log N) templates.

Where is it Used?

  • High-Throughput Engines: Permission Bitmasks: Managing User/Group/Other file access permissions (rwx).
  • System Resource Optimization: Cryptographic Hashing: Processing XOR and bit shift rounds in SHA-256 and AES encryption.

  • 2. Mental Model

    Imagine solving a complex puzzle where each piece has a predictable shape:

  • Once you identify the key pattern signal in the problem statement, you pull out the corresponding template.
  • You configure boundary invariants (such as left/right pointers, heap sizes, or stack monotonicity) and process elements in a single streamlined pass.

  • 3. Core Patterns & Implementations

    1. XOR Self-Inverse Element Isolation

    XOR all values in array. Since A ^ A = 0 and A ^ 0 = A, duplicate pairs cancel out leaving single number in O(N) time O(1) space.

    2. Brian Kernighan's Set Bit Count

    Execute n = n & (n - 1) in a loop to clear the lowest set bit, running in O(number of 1 bits) time.

    3. Bit Shift DP (Counting Bits)

    Compute set bits using past subproblems: dp[i] = dp[i >> 1] + (i & 1).

    4. Visual Trace


    5. Real-World Applications

  • Application 1: Permission Bitmasks: Managing User/Group/Other file access permissions (rwx).
  • Application 2: Cryptographic Hashing: Processing XOR and bit shift rounds in SHA-256 and AES encryption.

  • 6. Interview Perspective

    How Interviewers Ask This Topic

    Interviewers verify whether you recognize key problem constraints and select optimal patterns rather than defaulting to brute force.

    Common Mistakes

    Warning: 1. Using modulo or division instead of bitwise & 1 and >> 1 : Using modulo or division instead of bitwise & 1 and >> 1 — degrades performance in high-frequency loops.
    > 2. Forgetting bit operator precedence in Java/C++ (1 + 1 & 2 evaluates addition first without parentheses).: Forgetting bit operator precedence in Java/C++ (1 + 1 & 2 evaluates addition first without parentheses).

    7. Summary

  • Pattern 1: XOR Self-Inverse Element Isolation -> O(N) optimized pass.
  • Pattern 2: Brian Kernighan's Set Bit Count -> Invariant boundary handling.
  • Pattern 3: Bit Shift DP (Counting Bits) -> Optimal time and space efficiency.

  • 8. Quiz

    Question 1: What is the main time complexity advantage of using these patterns? Answer: They reduce nested loop brute force solutions (O(N^2) or higher) down to optimal linear O(N) or logarithmic O(N log N) bounds.
    Question 2: How do you choose between Pattern 1 and Pattern 2 during an interview? Answer: Look at problem invariants such as whether the input array is sorted, contiguous, or requires global bounds.
    Question 3: Why is space complexity critical in production environments for these patterns? Answer: In-place algorithms (O(1) auxiliary space) eliminate garbage collection overhead and prevent out-of-memory errors on large data streams.
    Question 4: True or False: You should always test edge cases (empty input, single element, negative values) before finishing code. Answer: True. Edge cases reveal hidden pointer out-of-bounds errors or division-by-zero crashes.
    Question 5: What is the best strategy when stuck on an interview problem? Answer: Walk through a small manual example, state the brute force solution, identify unnecessary repeated work, and apply one of these core patterns.