FunctionRacerProblemsRace

Reverse the Low Bits

EasyBitsPython, JavaScript, TypeScript, Java, C++

n is a 32 bit unsigned number with its bits numbered from the right, bit 0 being the lowest. Reverse the order of the lowest k bits, those at positions 0 through k - 1: bit 0 moves to position k - 1, bit 1 to position k - 2, and so on. The bits at positions k and above stay where they are. Return the resulting number as a non-negative integer. With k = 32 the whole number is reversed, so the result can be as large as 2**32 - 1.

Example 1

n 11
k 4
returns 13

11 is 1011; reversing all four low bits gives 1101, which is 13.

Example 2

n 6
k 2
returns 5

6 is 110; its lowest two bits 10 become 01, so the result is 101, which is 5.

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