Skip to content

Instantly share code, notes, and snippets.

@payal-kothari
Created October 9, 2017 20:34
Show Gist options
  • Select an option

  • Save payal-kothari/ee77d3d5e081e385cda80d8cb8519fe4 to your computer and use it in GitHub Desktop.

Select an option

Save payal-kothari/ee77d3d5e081e385cda80d8cb8519fe4 to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment