number of 1 bits

题目地址:https://leetcode.com/problems/number-of-1-bits/

解答:

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

 

posted @ 2015-04-07 17:06  buptubuntu  阅读(74)  评论(0)    收藏  举报