Two's complement of integer:

https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8

Bit Manipulation:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

 

代码:

public class Solution {
    public boolean isPowerOfTwo(int n) {
        int count = 0;
        if((n & Integer.MIN_VALUE) != 0){
            //n = ~n + 1;
            return false;
        } 
        for(int i = 32; i > 0; i--){
            count += n & 1;
            n = n>>1;
        }
        return count == 1;
    }
}

  

posted on 2016-01-13 05:17  爱推理的骑士  阅读(162)  评论(0编辑  收藏  举报