191. 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){
            res += (n & 1) == 1? 1:0;
            n >>>= 1;
        }
        
        return res;
    }
}
posted @ 2016-10-21 07:46  哇呀呀..生气啦~  阅读(66)  评论(0)    收藏  举报