码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-01-13 07:05:07      阅读:137      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!