Bit Manipulation — Complete Guide
Master bit manipulation: XOR tricks, counting bits, bitmask DP, Brian Kernighan, two's complement, and 5-language operators.
webcoderspeed.com
8 articles
Master bit manipulation: XOR tricks, counting bits, bitmask DP, Brian Kernighan, two's complement, and 5-language operators.
Every element appears twice except one. XOR all numbers: duplicates cancel (a^a=0), single number remains.
Two numbers appear once, rest appear twice. XOR all to get a^b, then use any set bit to partition numbers into two groups.
Find missing number in 0..n array. XOR approach: XOR all indices with all values, duplicate indices cancel. Or math: n*(n+1)/2 - sum.
Find maximum XOR of any two numbers in array. Binary trie (bit by bit from MSB): for each number greedily choose opposite bit to maximize XOR.
For each query (xi, mi), find max xi XOR with element ≤ mi. Offline: sort queries and elements by value, add elements up to mi, query trie.
Master bitwise operations for DSA: XOR tricks, Brian Kernighan bit counting, subset enumeration, and bitmask DP.
Advanced trie applications beyond basic insert/search: binary trie for XOR maximization, palindrome pairs detection, and stream search using Aho-Corasick automaton concept.