Last active
August 29, 2015 14:24
-
-
Save xynophon/04d56f421dc4163d33cc to your computer and use it in GitHub Desktop.
LeetCode. Number Of 1Bits. JDK7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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