LeetCode -- Power of Two

Given an integer, write a function to determine if it is a power of two.

Analysis: 给出一个整数,判断是否为2的幂方。

依次除以2,若某次中余数不为0,则不是二的幂方。

Answer:

    public boolean isPowerOfTwo(int n) {
        if(n <= 0)
            return false;
        while(n/2 !=0){
            if(n%2 != 0)
                return false;
            n = n/2;
        }

        return true;
    }
原文地址:https://www.cnblogs.com/little-YTMM/p/4784040.html