Build a Mask
positions lists bit positions, where position 0 is the lowest bit (worth 1), position 1 is worth 2, position 2 is worth 4, and so on.
Return the number whose binary form has a 1 at every listed position and 0 everywhere else. A position may be listed more than once, which changes nothing. An empty list gives 0.
Example 1
positions [0,2]
returns 5
Positions 0 and 2 are worth 1 and 4.
Example 2
positions [3,3]
returns 8
Position 3 is listed twice but counts once: 8.
Example 3
positions []
returns 0
No positions, no bits.
Constraints
0 <= len(positions) <= 200 <= positions[i] <= 29
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Fixed Width BinaryBeginner
- Flip One BitBeginner
- Gray Code SequenceBeginner
- How Many Bits LongBeginner
- Keep the Low BitsBeginner
- Lowest Set BitBeginner
- Parity BitBeginner
- Read One BitBeginner