XOR, AND, OR, and shifts for O(1) space solutions to number problems.
Bit manipulation solves problems at the binary level. XOR is the workhorse: x XOR x = 0 and x XOR 0 = x.
All bit operations O(1)
O(1)
Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.
a XOR a = 0, a XOR 0 = a; XOR all to find single element
n & (n - 1) removes the lowest set bit
n & (-n) isolates the rightmost set bit
(n >> k) & 1
set: n | (1 << k), clear: n & ~(1 << k)
XOR gives sum without carry; AND<<1 gives carry