Skip to content

Instantly share code, notes, and snippets.

@xynophon
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save xynophon/04d56f421dc4163d33cc to your computer and use it in GitHub Desktop.

Select an option

Save xynophon/04d56f421dc4163d33cc to your computer and use it in GitHub Desktop.
LeetCode. Number Of 1Bits. JDK7
import java.util.*;
public static int hammingWeight(int n) {
int cnt = 0;
while(n != 0){
if(0 != n%2){ //if(1 == n%2){
cnt++;
}
n = n >>> 1;
}
return cnt;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long num = sc.nextLong();
System.out.println(hammingWeight(num));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment