-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathNumberOf1Bits.java
44 lines (40 loc) · 1.07 KB
/
NumberOf1Bits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// TC:O(1) SC:O(1)
package java1.algorithms.binary;
public class NumberOf1Bits {
//Optimistic solution without scanning zeros
private static int hammingWeight(int num) {
int count = 0;
while(num != 0) {
num &= (num-1);
count++;
}
return count;
}
private static int hammingWeight1(int num) {
int count = 0;
while(num != 0) {
count += num % 2;
num = num >> 1;
}
return count;
}
private static int hammingWeight2(int num) {
int count = 0;
int mask = 1;
while(num != 0){
for(int i=0; i< 32; i++) {
if((num & mask) !=0) {
count++;
}
num = num >> 1;
}
}
return count;
}
public static void main(String[] args) {
int num = 11; // 00000000000000000000000000001011
System.out.println(hammingWeight(num));
System.out.println(hammingWeight1(num));
System.out.println(hammingWeight2(num));
}
}