cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

isPowerOfTwo

Posted on 2015-09-17 21:19  cKK  阅读(171)  评论(0编辑  收藏  举报
//Given an integer, write a function to determine if it is a power of two.
public class isPowerOfTwo {
    public static boolean isPowerOfTwo(int n) {
        if (n == 1)
            return true;
        else if (n < 0)
            return false;
        else {
            String str = Integer.toBinaryString(n);
            for(int i=1;i<str.length();i++)
            {
                if('1'==str.charAt(i))
                    return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
      System.out.println(isPowerOfTwo(1024));

    }
}