Bit Manipulation

Advanced & DP

O(1) low-level bitwise operations, masks, and XOR parity tricks

238 problems·45 Easy·110 Medium·83 Hard
Pattern Study Guide & Cheat Sheet
▼

Perform bitwise operations (&, |, ^, ~, <<, >>) to check, set, toggle, or count bits in O(1) time and space without allocating memory.

Core Invariant: Essential Bit Identities: 1) `x ^ x = 0` and `x ^ 0 = x` (cancels pairs); 2) `n & (n - 1)` clears the lowest set bit (Brian Kernighan); 3) `n & -n` isolates lowest set bit; 4) `(1 << k)` creates mask for k-th bit.

Recognize it (Keywords & Signals)
  • Single number (every element appears twice except one)
  • Number of 1 bits (Hamming weight)
  • Counting bits from 0 to N
  • Subsets represented as bitmasks (N <= 20)
  • Power of two verification
When NOT to use

Numbers exceed 64-bit integer limits and bitwise representations become complex strings, or problem requires decimal digits.

How to solve (Step-by-step)
  1. 1.Identify if problem involves duplicate cancellation (XOR), bit counting (`n & (n - 1)`), or subset mask (`1 << n`).
  2. 2.To clear lowest set bit: `n &= (n - 1)` in a loop until `n == 0`.
  3. 3.To check if k-th bit is set: `(n & (1 << k)) != 0`.
  4. 4.To toggle k-th bit: `n ^= (1 << k)`.
  5. 5.To check power of two: `n > 0 and (n & (n - 1)) == 0`.
Watch for (Interview Traps)
  • Operator precedence: `==` has higher precedence than `&` in Python! Always wrap `(n & (1 << i)) != 0` in parentheses
  • Negative numbers in Python: Python uses arbitrary-precision integers, so bit shifts on negative numbers require masking with `0xFFFFFFFF`
  • Off-by-one in bit shifts: 0-indexed vs 1-indexed bit positions
Brian Kernighan's Bit Counting & XOR Reduction
def single_number(nums: list[int]) -> int:
    # XOR Property: x ^ x = 0 and x ^ 0 = x
    # All duplicate pairs cancel out, leaving the unique single number
    unique = 0
    for x in nums:
        unique ^= x
    return unique

def count_set_bits(n: int) -> int:
    # Brian Kernighan's Algorithm: n & (n - 1) clears the lowest set bit
    count = 0
    while n > 0:
        n &= (n - 1)
        count += 1
    return count
Cost
O(1) or O(number of set bits) · O(1) strictly constant memory (Hardware-level instructions with blazing fast execution.)
Canonical problems
#136 Single Number: XOR all numbers to cancel duplicates in O(N) time and O(1) space
#191 Number of 1 Bits: Clear lowest bit with n & (n - 1) in loop
#231 Power of Two: n > 0 and (n & (n - 1)) == 0
#338 Counting Bits: DP on bits: dp[i] = dp[i >> 1] + (i & 1)
Medium·25 companies·Max freq 100%·Acc 0.8%
CoupangFiverrPornhub+22
Medium·24 companies·Max freq 90%·Acc 64.6%
NianticNikeNeetCode 150+21
Easy·22 companies·Max freq 84%·Acc 72.3%
Arista NetworksRevolutHCL+19
Easy·21 companies·Max freq 75%·Acc 77.9%
AirbnbAmdocstcs+18
Easy·19 companies·Max freq 63%·Acc 58.3%
WiproDeloitteGoogle+16
Hard·14 companies·Max freq 100%·Acc 50.5%
PinterestZScalerVirtu Financial+11
Easy·14 companies·Max freq 89%·Acc 77.1%
BoxVerkadaApple+11
Easy·13 companies·Max freq 76%·Acc 68.8%
QualcommAirbnbNvidia+10
Easy·12 companies·Max freq 76%·Acc 50.3%
QualcommSnapNvidia+9
Easy·12 companies·Max freq 58%·Acc 0.8%
QualcommNeetCode 150NeetCode 150+9
Medium·11 companies·Max freq 100%·Acc 55.8%
HuluSwiggyNeetCode 150+8
Medium·11 companies·Max freq 79%·Acc 61.6%
NeetCode 150NeetCode 150UiPath+8
Hard·9 companies·Max freq 100%·Acc 23.9%
ArcesiumSamsungTexas Instruments+6
Medium·9 companies·Max freq 100%·Acc 42.1%
RackspaceeBayPhonePe+6
Medium·8 companies·Max freq 100%·Acc 62.8%
Salesforce6senseNvidia+5
Medium·8 companies·Max freq 97%·Acc 44.7%
GeicoLinkedInMicrosoft+5
Medium·8 companies·Max freq 93%·Acc 63.7%
GeicoToptalGeneral Motors+5
Medium·8 companies·Max freq 75%·Acc 38.8%
LinkedInZeta GlobalAmazon+5
Medium·8 companies·Max freq 75%·Acc 20.0%
ZeptoSamsungMicrosoft+5
Medium·7 companies·Max freq 100%·Acc 45.9%
GuidewireGeicoAmerican Express+4
Easy·7 companies·Max freq 100%·Acc 0.7%
XXeBay+4
Hard·7 companies·Max freq 100%·Acc 54.8%
RokuPhonePeAirbnb+4
Easy·7 companies·Max freq 100%·Acc 88.0%
persistent systemsIBMMicrosoft+4
Medium·7 companies·Max freq 100%·Acc 75.9%
YelpTikTokMeta+4
Easy·7 companies·Max freq 100%·Acc 85.9%
Hudson River TradingHudson River TradingGoogle+4
Medium·7 companies·Max freq 88%·Acc 53.6%
GrammarlyLinkedInBloomberg+4
Medium·7 companies·Max freq 75%·Acc 56.9%
BNY MellontcsMicrosoft+4
Medium·7 companies·Max freq 68%·Acc 73.1%
DunzoAdobeGoogle+4
Medium·7 companies·Max freq 66%·Acc 0.7%
VisaHashedInRippling+4
Medium·7 companies·Max freq 65%·Acc 70.2%
SiemensZomatoBloomberg+4
Medium·7 companies·Max freq 65%·Acc 89.5%
BNY MellonCitadelGoogle+4
Medium·7 companies·Max freq 63%·Acc 48.5%
DE ShawGoogleGoldman Sachs+4
Hard·7 companies·Max freq 54%·Acc 82.9%
DatabricksAmazonMeta+4
Hard·6 companies·Max freq 100%·Acc 52.7%
thoughtspotWeRideUber+3
Medium·6 companies·Max freq 100%·Acc 64.8%
PaytmMicrosoftGoogle+3
Medium·6 companies·Max freq 100%·Acc 66.4%
Edelweiss GroupEdelweiss GroupMicrosoft+3
Medium·6 companies·Max freq 100%·Acc 65.4%
fourkitesBloombergMicrosoft+3
Medium·6 companies·Max freq 96%·Acc 52.6%
MathWorksCoupangAirbnb+3
Hard·6 companies·Max freq 91%·Acc 78.3%
ExpediaOracleServiceNow+3
Medium·6 companies·Max freq 88%·Acc 62.4%
AdobeIBMAmazon+3
Hard·6 companies·Max freq 75%·Acc 68.2%
DE ShawTuringGoogle+3
Medium·6 companies·Max freq 68%·Acc 61.2%
TrilogyGoldman SachsGoogle+3
Medium·6 companies·Max freq 67%·Acc 88.3%
YandexGoogleMeta+3
Hard·6 companies·Max freq 67%·Acc 27.2%
MeeshoDeutsche Banktcs+3
Medium·6 companies·Max freq 50%·Acc 80.4%
UberMicrosoftGoogle+3
Easy·6 companies·Max freq 44%·Acc 60.5%
AccentureGoogleBloomberg+3
Easy·6 companies·Max freq 42%·Acc 83.9%
IBMGoogleBloomberg+3
Medium·6 companies·Max freq 39%·Acc 73.8%
VisaGoogleBloomberg+3
Medium·6 companies·Max freq 38%·Acc 75.6%
MicrosoftGoldman SachsBloomberg+3
Medium·5 companies·Max freq 100%·Acc 30.2%
Squarepoint CapitalZomatoRippling+2
50 displayed (of 50 matching)Filtered: All Companies

Top Companies Testing Bit Manipulation

Browse company-specific interview problem lists and recency breakdowns:

Explore All 22 DSA Coding Patterns

Master core algorithmic patterns tested in FAANG & Big Tech coding interviews.

Patterns Hub

Bit Manipulation frequently asked questions

What is the Bit Manipulation pattern in coding interviews?

O(1) low-level bitwise operations, masks, and XOR parity tricks It is categorized under Advanced & DP and features 238 curated interview problems across Easy (45), Medium (110), and Hard (83) difficulties.

Which tech companies frequently test Bit Manipulation?

Companies frequently asking Bit Manipulation questions include Google, Amazon, Microsoft, Meta, Bloomberg, NeetCode 150.

How do I master the Bit Manipulation pattern?

Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.