dsa5 min read
Bit Manipulation — Complete Guide
Master bit manipulation: XOR tricks, counting bits, bitmask DP, Brian Kernighan, two's complement, and 5-language operators.
Read →
webcoderspeed.com
4 articles
Master bit manipulation: XOR tricks, counting bits, bitmask DP, Brian Kernighan, two's complement, and 5-language operators.
Count set bits (popcount). Brian Kernighan: n & (n-1) clears the lowest set bit; count how many operations until n=0.
Count set bits for all numbers 0..n. DP: dp[i] = dp[i >> 1] + (i & 1). Remove last bit and check if it was set.
Sum of Hamming distances between all pairs. For each bit position, count 0s and 1s: contribution = count_0 * count_1.