Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true.
Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
Credits:
Special thanks to @yukuairoy for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
1 public class Solution { 2 public boolean isPowerOfFour(int num) { 3 if(num == 1) return true; 4 if(num < 4) return false; 5 while(num%4 == 0){ 6 num = num >> 2; 7 } 8 if(num == 1) return true; 9 return false; 10 } 11 }