Skip to content

Commit 53a0fcb

Browse files
committed
feat(bit-manipulation, count-bits): count number of bits
1 parent 629a536 commit 53a0fcb

18 files changed

+105
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Number of 1 Bits
2+
3+
Write a function that takes a 32-bit binary representation of an unsigned integer n and returns the count of its 1 bits.
4+
5+
The binary representation of an unsigned integer is a sequence of 0s and 1s that represents the integer's value using
6+
base-2 notation. An example of the 32-bit binary representation of an unsigned integer 13 is 00000000000000000000000000001101.
7+
8+
## Constraint
9+
10+
- The input must be a 32-bit binary representation of an unsigned integer.
11+
12+
> Note: The input is passed to the function as an unsigned integer variable. In this lesson, we have displayed it as a
13+
> string of binary numbers solely for visualization purposes.
14+
15+
16+
## Solution
17+
18+
The first attempt to solve this would be to iterate over each bit in the 32-bit representation and keep increasing the
19+
count, representing the number of 1 bits, every time we encounter the digit 1. This is the correct way to solve this
20+
problem, and it even takes constant time and space complexity. However, this approach requires us to perform 32
21+
iterations regardless of the number of 1 bits, which makes it a less efficient approach, specifically for integers with
22+
a small number of 1 bits. For example, there are only three 1 bits in the unsigned integer 13, but we'll have to iterate
23+
the entire 32 times to get to this number.
24+
To avoid any unnecessary iterations, we can use bitwise manipulation to efficiently count the number of 1 bits in the
25+
given unsigned integer. Here, we'll iterate through the bits of the binary representation that are actually 1s. This
26+
means it only performs iterations equal to the number of 1 bits in the binary representation of the integer. For example,
27+
if the given integer is 12 with the binary representation of 00000000000000000000000000001100, the loop will iterate only
28+
twice, which is highly efficient.
29+
30+
First, we'll initialize a counter, count, to keep track of the number of 1 bits in the 32-bit binary representation of
31+
an unsigned integer. Then, we'll start a loop that continues as long as the integer n is not zero, i.e., there is no 1 bit
32+
left in the integer. This loop will iterate through each bit of the binary representation of n, starting from the least
33+
significant bit. In each iteration:
34+
35+
> The least significant bit is the rightmost bit in the binary representation of a number.
36+
37+
- Check whether the least significant bit of n is equal to 1 using the bitwise AND operation.
38+
- If the last bit is 1, increment the count by 1.
39+
- Otherwise, continue with the loop.
40+
- Once we are done checking the current bit, right-shift n by one position using the right bitwise shift operator. This
41+
operation effectively shifts all bits one place to the right, discarding the last bit and making the next bit the new
42+
least significant bit.
43+
44+
Eventually, count will have the total number of 1 bits, so we'll return it as the final result.
45+
46+
Let’s look at the following illustration to get a better understanding of the solution:
47+
48+
![Solution 1](./images/solutions/number_of_one_bits_solution_1.png)
49+
![Solution 2](./images/solutions/number_of_one_bits_solution_2.png)
50+
![Solution 3](./images/solutions/number_of_one_bits_solution_3.png)
51+
![Solution 4](./images/solutions/number_of_one_bits_solution_4.png)
52+
![Solution 5](./images/solutions/number_of_one_bits_solution_5.png)
53+
![Solution 6](./images/solutions/number_of_one_bits_solution_6.png)
54+
![Solution 7](./images/solutions/number_of_one_bits_solution_7.png)
55+
![Solution 8](./images/solutions/number_of_one_bits_solution_8.png)
56+
![Solution 9](./images/solutions/number_of_one_bits_solution_9.png)
57+
![Solution 10](./images/solutions/number_of_one_bits_solution_10.png)
58+
![Solution 11](./images/solutions/number_of_one_bits_solution_11.png)
59+
![Solution 12](./images/solutions/number_of_one_bits_solution_12.png)
60+
![Solution 13](./images/solutions/number_of_one_bits_solution_13.png)
61+
![Solution 14](./images/solutions/number_of_one_bits_solution_14.png)
62+
![Solution 15](./images/solutions/number_of_one_bits_solution_15.png)
63+
64+
### Time Complexity
65+
66+
In the worst case, the number of iterations in the loop can go up to 32, which is a fixed number. Therefore, the overall
67+
time complexity for this solution is O(1).
68+
69+
### Space Complexity
70+
71+
The space complexity of this solution is O(1).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def count_bits(n: int) -> int:
2+
count = 0
3+
4+
while n:
5+
# Check the least significant bit by using AND
6+
if n & 1:
7+
count += 1
8+
# Right-shift the number to move to the next bit
9+
n >>= 1
10+
11+
return count
45.4 KB
Loading
50.4 KB
Loading
43.9 KB
Loading
66.1 KB
Loading
50.9 KB
Loading
44.7 KB
Loading
60.7 KB
Loading
41.7 KB
Loading

0 commit comments

Comments
 (0)