Plain and Easy understanding with loops to count trailing zeros (Java)


  • 4
    L
       public boolean isPowerOfFour(int num) {
            if (num <= 0) return false;
            int count = 0;
            int temp = num;
            while (temp > 0) {
                if ((temp & 1) == 0) {
                    count++;
                } else {
                    break;
                }
                temp = temp >> 1;
            }
            return (count % 2 == 0) && ((num & (num - 1)) == 0);
        }

Log in to reply
 

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