-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountBits.java
More file actions
35 lines (31 loc) · 943 Bytes
/
Copy pathCountBits.java
File metadata and controls
35 lines (31 loc) · 943 Bytes
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
package epi.primitive;
import static org.junit.Assert.assertEquals;
/**
* Question:
* Count the number of bits that are set to 1 in an integer.
* ---
* Solution:
* We start iterating through each bit in the integer, starting from right to left.
* When one bit is processed using a 1 MASK, unsigned right-shift operation is applied
* in order to advance to the next left bit.
* ---
* Time Complexity: O(n) -> n is the number of bits in the integer
* Space Complexity: O(1)
*/
public class CountBits {
public static void main(String[] args) {
assertEquals(6, countBits(123));
}
private static short countBits(int x) {
short count = 0;
while (x != 0) {
// Check if last bit is 1
if ((x & 1) == 1) {
count++;
}
// Unsigned right shift x in order to process the next bit
x >>>= 1;
}
return count;
}
}