Skip to content

Commit 9bc8c66

Browse files
authored
Create count_set_bits.cpp
1 parent aee0135 commit 9bc8c66

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

count_set_bits.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int countSetBits(int n)
5+
{
6+
int count = 0;
7+
while (n) {
8+
count += (n & 1);
9+
n >>= 1;
10+
}
11+
return count;
12+
}
13+
14+
int main()
15+
{
16+
int n = 10;
17+
cout << countSetBits(n);
18+
return 0;
19+
}

0 commit comments

Comments
 (0)