FunctionRacerProblemsRace

Build a Mask

BeginnerBitsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems