Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C/C++ Program to Count set bits in an integer?
Here we will see how we can count the number of set bits in an integer. Set bits are the bits that have a value of 1 in the binary representation of a number. For example, the number 13 has binary representation 1101, which contains three set bits, so the count will be 3.
To solve this problem, we will shift the number to the right and check if the least significant bit (LSB) is 1. If it is, we increment our count. This process continues until the number becomes 0.
Syntax
int countSetBits(int n);
Algorithm
begin
count := 0
while n is not 0, do
if LSB of n is set, then
count := count + 1
end if
n := n after shifting 1 bit to right
done
return count
end
Method 1: Using Right Shift and Bitwise AND
This approach checks each bit by shifting the number right and using bitwise AND operation −
#include <stdio.h>
int countSetBits(int n) {
int count = 0;
while (n != 0) {
if (n & 1) {
count++;
}
n = n >> 1;
}
return count;
}
int main() {
int num = 29;
printf("Number: %d\n", num);
printf("Binary: ");
/* Display binary representation */
for (int i = 31; i >= 0; i--) {
printf("%d", (num >> i) & 1);
if (i % 4 == 0) printf(" ");
}
printf("\nNumber of set bits: %d\n", countSetBits(num));
return 0;
}
Number: 29 Binary: 0000 0000 0000 0000 0000 0000 0001 1101 Number of set bits: 4
Method 2: Brian Kernighan's Algorithm
This optimized approach uses the property that n & (n-1) removes the rightmost set bit −
#include <stdio.h>
int countSetBitsBK(int n) {
int count = 0;
while (n) {
n = n & (n - 1);
count++;
}
return count;
}
int main() {
int numbers[] = {13, 29, 7, 15, 0};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Number\tBinary\t\tSet Bits\n");
printf("------\t------\t\t--------\n");
for (int i = 0; i < size; i++) {
printf("%d\t", numbers[i]);
/* Display 8-bit binary */
for (int j = 7; j >= 0; j--) {
printf("%d", (numbers[i] >> j) & 1);
}
printf("\t\t%d\n", countSetBitsBK(numbers[i]));
}
return 0;
}
Number Binary Set Bits ------ ------ -------- 13 00001101 3 29 00011101 4 7 00000111 3 15 00001111 4 0 00000000 0
Comparison
| Method | Time Complexity | Space Complexity | Iterations |
|---|---|---|---|
| Right Shift Method | O(log n) | O(1) | 32 (for 32-bit integer) |
| Brian Kernighan's Algorithm | O(k) | O(1) | k (number of set bits) |
Key Points
- Brian Kernighan's algorithm is more efficient as it only iterates for the number of set bits.
- The expression
n & (n-1)always removes the rightmost set bit. - Both methods work for positive integers. For negative numbers, consider using unsigned int.
Conclusion
Counting set bits can be done efficiently using bitwise operations. Brian Kernighan's algorithm provides the optimal solution by reducing the number of iterations to match the actual count of set bits.
