Bit Manipulation
Use binary representations and bitwise operations to solve compact state and parity problems.
The task mentions bits, parity, powers of two, compact subsets, or one unique value among duplicates.
How to approach it
Write down the bit pattern, apply one operation at a time, and use masks to isolate or toggle selected bits.
Signed integers and shift behavior vary across languages; know the integer width and sign rules.
Practice question
Every number appears twice except one. What remains after XORing [4, 1, 2, 1, 2]?
values = [4, 1, 2, 1, 2]Give your answer before revealing the explanation.Show a hint
XOR is associative and commutative, and x XOR x = 0.
Show an answer
4, because duplicate pairs cancel under XOR.
Bit Manipulation at work
A device stores four on/off settings in one integer. If the third setting uses mask 0100, how can it check whether that setting is enabled?
Show one possible approach
Compute flags AND 0100 and check whether the result is nonzero. The mask isolates only the third bit.
Practice next
Use these problem IDs to find additional practice for this pattern: