首页 > 其他 > 详细

Jan 12 - Power of Two; Integer; Bit Manipulation;

时间:2016-01-13 07:04:30      阅读:132      评论:0      收藏:0      [点我收藏+]

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;
    }
}

  

Jan 12 - Power of Two; Integer; Bit Manipulation;

原文:http://www.cnblogs.com/5683yue/p/5126123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!