FunctionRacerProblemsRace

Switch Off the Lowest One

BeginnerBitsPython, JavaScript, TypeScript, Java, C++

Return n with its lowest 1 bit switched off. Every other bit stays as it is. If n is 0 there is nothing to switch off, and the answer is 0.

For example 12 is 1100 in binary. Its lowest 1 is the one worth 4, so the answer is 1000, which is 8.

Example 1

n 12
returns 8

12 is 1100. Without its lowest 1 it is 1000.

Example 2

n 7
returns 6

7 is 111. Without its lowest 1 it is 110.

Example 3

n 16
returns 0

A power of two has a single 1, so nothing is left.

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