File tree Expand file tree Collapse file tree 1 file changed +5
-0
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +5
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ public static class Solution1 {
7
7
* Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero
8
8
* example run for the above editorial solution: input 5, n will be 5&4 and becomes 4,
9
9
* then in the next run, n will become 4&3 which is 0, thus exit the while loop.
10
+ *
11
+ * e.g. 5 in binary format: 101
12
+ * 4 in binary format: 100
13
+ * so 5 AND 4 becomes 100, it's the least significant 1-bit in n always maps to a 0-bit in n - 1
14
+ * so n ANDs with n - 1, will just set the least significant 1-bit in n to be zero and all other bits remain unchanged
10
15
*/
11
16
public int hammingWeight (int n ) {
12
17
int bits = 0 ;
You can’t perform that action at this time.
0 commit comments