Created
October 9, 2017 20:34
-
-
Save payal-kothari/ee77d3d5e081e385cda80d8cb8519fe4 to your computer and use it in GitHub Desktop.
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
| 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