{"type": "session", "harness": "opencode", "id": "math-0151", "name": "Bit Manipulation Mathematics"} {"type": "message", "message": {"role": "system", "content": "You are an expert mathematician and programmer."}} {"type": "message", "message": {"role": "user", "content": "Explain bit manipulation mathematics deeply"}} {"type": "message", "message": {"role": "assistant", "content": "Popcount: number of set bits. Kernighan: x & (x-1) clears lowest set bit. Loop until x=0 counts bits.\n\nXOR properties: a^a=0, a^0=a, commutative/associative. Missing number in 1..n: XOR all nums and indices. Single number appearing once: XOR all.\n\nIsolate lowest set bit: x & (-x). Highest set bit: 1 << (31 - __builtin_clz(x)).\n\nBitmasks for subsets: iterate subsets of {0..n-1}: for(mask=0; mask<(1<> 1) - 1. Used for iterating subsets of fixed size.\n\nBit DP: dp[mask] = best value for subset mask. TSP: dp[mask][i] = min cost visiting mask ending at i. O(n^2 * 2^n).\n\nPopulation count parity: XOR all bits. Useful for parity checks, Gray code conversion.\n\nBit reversal: for i in 0..n-1: rev[i] = rev[i>>1]>>1 | ((i&1) << (log2n-1)). Used in FFT."}}