tinycodinglm / math /math-0152.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.01 kB
opencode trace
System You are an expert mathematician and programmer.

You are an expert mathematician and programmer.

User

Explain bit manipulation mathematics

Assistant

Popcount: number of set bits. Kernighan: x & (x-1) clears lowest set bit. Loop until x=0.
XOR properties: a^a=0, a^0=a, commutative/associative. Missing number: XOR all nums and indices.
Bitmasks for subsets: iterate subsets of {0..n-1}: for(mask=0; mask<(1<<n); mask++) for(i=0; i<n; i++) if(mask&(1<<i)).
Subsets of subsets: for(sub=mask; sub; sub=(sub-1)&mask) iterates all non-empty submasks.
Single-bit operations: isolate lowest bit (x&(-x)), position (__builtin_ctz), count (__builtin_popcount).
Bit DP: dp[mask] = best value for subset represented by mask. TSP: dp[mask][i] = min cost visiting nodes in mask ending at i.