FunctionRacerProblemsRace

Round Up to a Power of Two

EasyBitsPython, JavaScript, TypeScript, Java, C++

Return the smallest power of two (1, 2, 4, 8, 16, ...) that is greater than or equal to the positive integer n. When n is itself a power of two, the answer is n.

Example 1

n 5
returns 8

The powers of two around 5 are 4 and 8; 8 is the first one that is at least 5.

Example 2

n 8
returns 8

8 is already a power of two.

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