![]() |
VOOZH | about |
Given a positive number n count the set bit .
Examples :
Input : n = 6
Output : 2
Binary representation of 6 is 110 and has 2 set bitsInput : n = 13
👁 setbit
Output : 3
Binary representation of 13 is 1101 and has 3 set bits
Table of Content
This approach counts the set bits by checking each bit from right to left. It uses n & 1 to check if the least significant bit is 1, increments the count if so, then right-shifts the number to check the next bit. This repeats until n becomes 0, returning the total number of set bits.
2
Time Complexity: O(log n)
Auxiliary Space: O(1)
Recursive Implementation The function checks if the least significant bit is 1 and adds to the count. It then right-shifts the number and repeats this process recursively until all bits are checked, returning the total count of set bits.
2
Time Complexity: O(log n)
Auxiliary Space: O(log n)for recursive stack space
Brian Kernighan’s algorithm counts set bits efficiently by clearing them one by one. The key operation is n &= (n - 1), which removes the least significant set bit from n. Each time this is done, it means one set bit has been counted and removed. The process repeats until n becomes 0, and the total number of times the loop runs gives the count of set bits. This is faster than checking each bit individually because it only iterates as many times as there are set bits in the number.
Subtracting 1 from a decimal number flips all the bits after the rightmost set bit(which is 1) including the rightmost set bit.
for example :
10 in binary is 00001010
9 in binary is 00001001
8 in binary is 00001000
7 in binary is 00000111
So if we subtract a number by 1 and do it bitwise & with itself (n & (n-1)), we unset the rightmost set bit. If we do n & (n-1) in a loop and count the number of times the loop executes, we get the set bit count.
The beauty of this solution is the number of times it loops is equal to the number of set bits in a given integer.
1 Initialize count: = 0
2 If integer n is not zero
(a) Do bitwise & with (n-1) and assign the value back to n
n: = n&(n-1)
(b) Increment count by 1
(c) go to step 2
3 Else return count
2
Time Complexity: O(log n)
Auxiliary Space: O(1)
It first builds a table BitsSetTable256[] storing the count of set bits for all 8-bit numbers (0–255). In countSetBits, the 32-bit number is divided into four 8-bit chunks, and the table is used to quickly find the set bits in each byte. Summing these gives the total set bit count. This method is efficient because it replaces bitwise loops with quick table lookups, making it ideal when frequent set bit counting is needed.
2
Time Complexity: O(1)
Auxiliary Space: O(1)
It simply maintains a map(or array) of numbers to bits for a nibble. A Nibble contains 4 bits. So we need an array of up to 15.
int num_to_bits[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
Now we just need to get nibbles of a given long/int/word etc recursively.
2
Time Complexity: O(log n), because we have log(16, n) levels of recursion.
Storage Complexity: O(1) Whether the given number is short, int, long, or long long we require an array of 16 sizes only, which is constant.
Iterate from k to 0 , where k is the largest power of 2 such that pow(2, k) <= num . And check if the Bitwise AND of num and pow(2, i) is greater than zero or not. If it is greater than zero , Then i-th bit is set ,then increase the count by 1.
2
Time Complexity: O(logn)
Auxiliary Space: O(1)
In C++ GCC, we can directly count set bits using __builtin_popcount(). So we can avoid a separate function for counting set bits. Similarly, we have direct syntax in other programming languages.
4