Java Simple 1-line solution and easy to understand


  • 0
    S
    public boolean isPowerOfFour(int num) {
        return num >=0 && Integer.bitCount(num) == 1 && (Integer.bitCount(num-1) & 1) == 0;
    }
    

    If num is the power of 4, the one bit number of (num-1) is even.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.