Power of Two

Easy~10 min

Given an integer n, return true if it is a power of two, and false otherwise.

An integer n is a power of two if there exists an integer k such that n == 2^k. Note that 1 is a power of two (2^0 = 1). Zero and negative numbers are never powers of two.

Examples

Example 1
Input: n = 1
Output: true
Explanation: 2^0 = 1, so 1 is a power of two.
Example 2
Input: n = 16
Output: true
Explanation: 2^4 = 16, so 16 is a power of two.
Example 3
Input: n = 3
Output: false
Explanation: There is no integer k such that 2^k = 3.

Constraints

  • -2^31 <= n <= 2^31 - 1
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run