payal-kothari
10/9/2017 - 8:34 PM

Number of 1 Bits (leetcode 191)

public class Solution {
    // you need to treat n as an unsigned value
    public static int hammingWeight(int n) {
	    int count = 0;
    	while(n!=0) {
    		int check = n & 1;
            count = count + check;
    		n = n>>>1;       // zero filled right shift, eventually the no. "n" will become zero
    	}
    	return count;
    }
}