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 verifiedO(N) or O(N log N) templates.
Where is it Used?
2. Mental Model
Imagine solving a complex puzzle where each piece has a predictable shape:
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
Executen = 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
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 & 2evaluates addition first without parentheses).: Forgetting bit operator precedence in Java/C++ (1 + 1 & 2evaluates addition first without parentheses).