Check, set, clear, and toggle
Choose a bitwise operation from its one-bit truth table.
Understand the idea
AND keeps a bit only if both operands have it; OR keeps a bit if either has it; XOR keeps a bit when they differ. For b = 1ULL << k, test (mask & b) != 0, set with mask |= b, clear with mask &= ~b, and toggle with mask ^= b.
Trace a small example
Starting at binary 1010, setting bit 0 gives 1011. Clearing bit 3 gives 0011, and toggling bit 1 gives 0001. Bitwise & examines all bits; logical && converts operands to truth values and short-circuits.
Conditions and cost
Use parentheses around bit tests in larger expressions. A complement flips every bit of the type, so intersect it with a universe mask when only the lowest n bits are meaningful. Do not compute (1ULL << n) when n equals the type width.
Prefix sum explorer
Query [l, r] is zero-based and inclusive. P[k] stores the total of the first k values; P[0] is zero.
Input array a
Prefix array P
Preprocess once in O(n). Each range sum then takes O(1).
Guided simulation · Change the inputs to test your understanding.
Which operation clears bit k while preserving the others?
Turn the idea into code
Start with mask 10. Set bit 0, then toggle bit 3. What value remains?
Give me a hint
10 is binary 1010.
Show the explained solution
Setting bit 0 gives 11 (1011); toggling bit 3 gives 3 (0011).
Your notes
Mark complete when you can explain it yourself.