File tree 1 file changed +5
-9
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
- /**
4
- * 191. Number of 1 Bits
5
- *
6
- * Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
7
- * For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.*/
8
-
9
3
public class _191 {
10
4
11
5
public static class Solution1 {
12
- /**Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero
13
- example run for the above editorial solution: input 5, n will be 5&4 and becomes 4,
14
- then in the next run, n will become 4&3 which is 0, thus exit the while loop.*/
6
+ /**
7
+ * Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero
8
+ * example run for the above editorial solution: input 5, n will be 5&4 and becomes 4,
9
+ * then in the next run, n will become 4&3 which is 0, thus exit the while loop.
10
+ */
15
11
public int hammingWeight (int n ) {
16
12
int bits = 0 ;
17
13
while (n != 0 ) {
You can’t perform that action at this time.
0 commit comments