Given an integer n, return true if it is a power of four. Otherwise, return false.
An integer n is a power of four, if there exists an integer x such that n == 4x.
Example 1:
1 | Input: n = 16 |
Example 2:
1 | Input: n = 5 |
Example 3:
1 | Input: n = 1 |
Constraints:
1 | -231 <= n <= 231 - 1 |
Approach
1 | If an integer is a power of four, only one bit is 1, others will be 0. |
Algorithm
1 | Check if the bit is an even bit and make sure only one bit is 1. |
Implementation
1 | bool isPowerOfFour(int n){ |