Number of 1 Bits

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int res = 0;
        while(n!=0){
            if((n&1)==1){
                res++;
            }
                n>>>=1;
        }
        return res;
    }
}

看了reverse bites就很容易理解这道题了

 

posted @ 2015-05-22 03:48  世界到处都是小星星  阅读(93)  评论(0编辑  收藏  举报