Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions bit_manipulation/count_set_bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// count_set_bits.cpp
// Author: Abhijeet Kundu
// Description: Counts number of set bits (1s) in the binary representation of an integer
// using Brian Kernighan’s Algorithm.
// Time Complexity: O(k) where k is number of set bits

#include <iostream>
#include <cassert>

int countSetBits(int n) {
int count = 0;
while (n) {
n &= (n - 1); // clears the least significant set bit
count++;
}
return count;
}

int main() {
assert(countSetBits(0) == 0);
assert(countSetBits(5) == 2); // 101 → 2 set bits
assert(countSetBits(15) == 4); // 1111 → 4 set bits

int n;
std::cout << "Enter a number: ";
std::cin >> n;
std::cout << "Number of set bits: " << countSetBits(n) << "\n";
return 0;
}