1271. Check if a Number is a Power of Two

EasyBit ManipulationBit ManipulationPower of TwoBit Trick

Given an integer n, determine whether it is a power of two (that is, n = 2^x for some non-negative integer x). Return true if it is, otherwise false. Zero and negative numbers are not powers of two.

Input: A JSON object {"n": <integer>} which may be zero or negative.

Output: Return true if n is a power of two, otherwise false.

Examples

Example 1
Input: {"n":16}
Output: true
Explanation: 16 is 10000 with one set bit -> true.
Example 2
Input: {"n":18}
Output: false
Explanation: 18 is 10010 with two set bits -> false.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →