One line Java, No complex coding, Using only Integer class methods


  • 5

    For a number to be power of 4, it should-

    1. Have all 0 bits and 1 - 1bit. eg 100,10000
    2. Zeros after 1 should be even. One zero mean multiple of 2. 2 zero means multiple of 4.
    public boolean isPowerOfFour(int num) {
            return Integer.bitCount(num) == 1 && (Integer.toBinaryString(num).length()-1)%2==0;
        }

Log in to reply
 

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