|
| 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 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 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). |
0 commit comments