Skip to main content

πŸ”’ Bit Manipulation

One-line summary: Operate directly on bits β€” XOR, AND, OR, shifts β€” for elegant O(1) tricks that would otherwise require O(n) logic.


Diagram​

Bit Manipulation Overview Bit Manipulation GIF

Concept​

OperationSymbolExampleResult
AND&5 & 31
OR|5 | 37
XOR^5 ^ 36
NOT~~5-6
Left shift<<5 << 110
Right shift>>5 >> 12

Key XOR properties: a^a=0, a^0=a, commutative, associative.


Common Patterns​

Find Single Number​

const singleNumber = (nums) => nums.reduce((acc, n) => acc ^ n, 0);

Check Even/Odd​

const isEven = (n) => (n & 1) === 0;

Count Set Bits (Brian Kernighan)​

function countBits(n) {
let count = 0;
while (n > 0) {
n &= n - 1;
count++;
}
return count;
}

XOR Swap​

let a = 5,
b = 3;
a ^= b;
b ^= a;
a ^= b;

Power of Two Check​

const isPowerOfTwo = (n) => n > 0 && (n & (n - 1)) === 0;

Pitfalls​

  • JS bitwise ops work on 32-bit signed integers β€” large numbers get truncated
  • ~n = -(n+1) β€” use >>> 0 to convert to unsigned if needed
  • XOR swap doesn't work if a and b point to the same variable

Practice Problems​

Easy Problems​

ProblemDifficultySolution
LC 136 β€” Single NumberEasy
LC 191 β€” Number of 1 BitsEasy
LC 231 β€” Power of TwoEasy
LC 268 β€” Missing NumberEasy
LC 338 β€” Counting BitsEasy
LC 342 β€” Power of FourEasy
LC 389 β€” Find the DifferenceEasy
LC 401 β€” Binary WatchEasy
LC 405 β€” Convert a Number to HexadecimalEasy
LC 461 β€” Hamming DistanceEasy
LC 476 β€” Number ComplementEasy
LC 693 β€” Binary Number with Alternating BitsEasy
LC 762 β€” Prime Number of Set BitsEasy
LC 832 β€” Flipping an ImageEasy
LC 868 β€” Binary GapEasy
LC 1009 β€” Complement of Base 10 IntegerEasy
LC 1290 β€” Convert Binary Number in a Linked List to IntegerEasy
LC 1342 β€” Number of Steps to Reduce a Number to ZeroEasy
LC 1356 β€” Sort Integers by The Number of 1 BitsEasy
LC 1486 β€” XOR Operation in an ArrayEasy
LC 1720 β€” Decode XORed ArrayEasy
LC 2220 β€” Minimum Bit Flips to Convert NumberEasy
LC 2239 β€” Find Closest Number to ZeroEasy
CC β€” Little Elephant and Bits (LEBITS)Easy
CC β€” Bit Difference (BITDIFF)Easy
CC β€” Count Set Bits (CNTSETBIT)Easy
CC β€” XOR Basics (XORBASIC)Easy

Medium Problems​

ProblemDifficultySolution
LC 137 β€” Single Number IIMedium
LC 190 β€” Reverse BitsMedium
LC 201 β€” Bitwise AND of Numbers RangeMedium
LC 260 β€” Single Number IIIMedium
LC 287 β€” Find the Duplicate NumberMedium
LC 318 β€” Maximum Product of Word LengthsMedium
LC 371 β€” Sum of Two IntegersMedium
LC 393 β€” UTF-8 ValidationMedium
LC 421 β€” Maximum XOR of Two Numbers in an ArrayMedium
LC 477 β€” Total Hamming DistanceMedium
LC 645 β€” Set MismatchMedium
LC 898 β€” Bitwise ORs of SubarraysMedium
LC 1310 β€” XOR Queries of a SubarrayMedium
LC 1318 β€” Minimum Flips to Make a OR b Equal to cMedium
LC 1442 β€” Count Triplets That Can Form Two Arrays of Equal XORMedium
LC 1521 β€” Find a Value of a Mysterious Function Closest to TargetMedium
LC 1680 β€” Concatenation of Consecutive Binary NumbersMedium
LC 1734 β€” Decode XORed PermutationMedium
LC 1829 β€” Maximum XOR for Each QueryMedium
LC 1835 β€” Find XOR Sum of All Pairs Bitwise ANDMedium
CC β€” XOR Engine (XORENG)Medium
CC β€” AND OR Union (ANDORUN)Medium
CC β€” Subset XOR (SUBSETXOR)Medium
CC β€” Bit Manipulation Tricks (BITTRICK)Medium

Hard Problems​

ProblemDifficultySolution
LC 51 β€” N-QueensHard
LC 52 β€” N-Queens IIHard
LC 115 β€” Distinct SubsequencesHard
LC 1178 β€” Number of Valid Words for Each PuzzleHard
LC 1255 β€” Maximum Score Words Formed by LettersHard
LC 1542 β€” Find Longest Awesome SubstringHard
LC 1659 β€” Maximize Grid HappinessHard
LC 1707 β€” Maximum XOR With an Element From ArrayHard
LC 1803 β€” Count Pairs With XOR in a RangeHard
LC 1915 β€” Number of Wonderful SubstringsHard
LC 2003 β€” Smallest Missing Genetic Value in Each SubtreeHard
CC β€” Complex Bit Operations (COMPLEXBIT)Hard
CC β€” Bit Masking DP (BITMASKDP)Hard
CC β€” Trie with XOR (TRIEXOR)Hard
CC β€” Advanced Bitwise (ADVBIT)Hard

← Back to Home Β· Β© sparshjaswal