|
1 | 1 | packagecom.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 | publicclass_191 {
|
10 | 4 |
|
11 | 5 | publicstaticclassSolution1 {
|
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 | publicinthammingWeight(intn) {
|
16 | 12 | intbits =0;
|
17 | 13 | while (n !=0) {
|
|