1295. Round Down to Nearest Power of Two

EasyBit ManipulationBit ManipulationPower of TwoHighest Set Bit

Given a non-negative integer n, return the largest power of two that is less than or equal to n (that is, keep only the highest set bit). If n is 0 there is no such power, so return 0.

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

Output: Return the largest power of two <= n, or 0 when n is 0.

Examples

Example 1
Input: {"n":19}
Output: 16
Explanation: The highest power of two not exceeding 19 is 16.
Example 2
Input: {"n":16}
Output: 16
Explanation: 16 is already a power of two, so it rounds down to itself.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →