DSAverse
Bit Manipulation
Loading Bit Manipulation...
Binary Register
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Bit Manipulation
Loading Bit Manipulation...
Binary Register
Master the art of working directly with bits. These O(1) and O(n) tricks unlock elegant solutions that would otherwise require loops or extra space.
a ^ a = 0, a ^ 0 = a. XOR is commutative and associative — perfect for cancelling duplicates.
Clears the lowest set bit of n. Applying repeatedly counts set bits; checking for zero detects powers of two.
Bitwise operations execute in a single CPU cycle, making bit tricks some of the fastest algorithms possible.
Explore bitwise tricks with step-by-step binary animations
Find the one element that appears only once in an array where every other element appears twice. XOR cancels duplicate pairs in a single linear pass.
O(n)O(1)Count the number of 1-bits in an integer using Brian Kernighan's algorithm. Each iteration n & (n−1) clears the lowest set bit, running in O(set bits) time.
O(log n)O(1)Check whether a number is a power of two using the elegant n & (n−1) == 0 trick. Powers of two have exactly one set bit — flipping it produces zero.
O(1)O(1)The secret weapon of competitive programmers and systems engineers
Bit tricks appear in FAANG interviews as elegant one-liners that replace O(n) space with O(1). Recognizing the pattern is half the battle.
Flags, masks, and packed data are everywhere in OS kernels, network protocols, graphics, and embedded systems.
Bitmask DP, subset enumeration, and fast popcount are essential tools for competitive programming problems with tight constraints.