FunctionRacerProblemsRace

Keep the Low Bits

BeginnerBitsPython, JavaScript, TypeScript, Java, C++

Return the number made of only the lowest k bits of n. All higher bits are thrown away.

For example 45 is 101101 in binary. Its lowest 3 bits are 101, so with k = 3 the answer is 5. With k = 0 no bits are kept and the answer is 0.

Example 1

n 45
k 3
returns 5

45 is 101101. The lowest three bits are 101.

Example 2

n 45
k 4
returns 13

The lowest four bits of 101101 are 1101.

Example 3

n 6
k 10
returns 6

Keeping more bits than the number has changes nothing.

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