Math Fundamentals18 sections · 814 units
Open in Course

Bit Manipulation Tricks Summary

Quick reference

Here's a quick reference of the most useful bit tricks:

• Power of 2 check: n>0n > 0 and n&(n1)=0n \& (n-1) = 0

• Clear rightmost set bit: n&(n1)n \& (n-1)

• Isolate rightmost set bit: n&(n)n \& (-n)

• Count set bits: loop with n:=n&(n1)n := n \& (n-1)

• Find unique element (all others appear twice): XOR all numbers

• Generate all subsets: iterate masks from 00 to 2n12^n - 1

Memorize these patterns. They appear frequently in coding interviews.