1296. Round Up to Nearest Power of Two

EasyBit ManipulationBit ManipulationPower of TwoHighest Set Bit

Given an integer n, return the smallest power of two that is greater than or equal to n. For n <= 1 the answer is 1. If n is already a power of two it rounds up to itself.

Input: A JSON object {"n": <integer>}.

Output: Return the smallest power of two >= n (and 1 for n <= 1).

Examples

Example 1
Input: {"n":17}
Output: 32
Explanation: The smallest power of two >= 17 is 32.
Example 2
Input: {"n":16}
Output: 16
Explanation: 16 is already a power of two -> 16.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →