About This Pattern
Master the Bit Manipulation pattern by practicing these problems organized by difficulty.Focus on the Key Signals - these are the indicators that tell you when to use this pattern.
Easy (2)
Number of 1 Bits
Count the number of 1 bits in an integer (Hamming weight).
Key Signals:
Check each bitn & 1 for last bitShift right or use n & (n-1)
Reverse Bits
Reverse the bits of a 32-bit unsigned integer.
Key Signals:
Build result bit by bitExtract from right, add to leftShift both directions
Medium (2)
Sum of Two Integers
Add two integers without using + or - operators.
Key Signals:
XOR for sum without carryAND << 1 for carryRepeat until no carry
Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
Key Signals:
Bit shiftingOverflow handlingDivision without operators