Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 43a2477

Browse files
committedOct 12, 2021
refactor 191
1 parent c8ca38e commit 43a2477

File tree

1 file changed

+5
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+5
-0
lines changed
 

‎src/main/java/com/fishercoder/solutions/_191.java

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ public static class Solution1 {
77
* Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero
88
* example run for the above editorial solution: input 5, n will be 5&4 and becomes 4,
99
* 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
1015
*/
1116
public int hammingWeight(int n) {
1217
int bits = 0;

0 commit comments

Comments
 (0)
Failed to load comments.