We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c5949b6 commit ca59d96Copy full SHA for ca59d96
General Programs/SetBits.cpp
@@ -3,7 +3,44 @@
3
//program to count number of set bits
4
using namespace std;
5
6
+/*
7
+1) Using & operator right shift operation
8
+2) Using __buildtin_popcount(n) function to get count of set bits
9
+3)Brian Kernighan’s Algorithm:
10
+*/
11
+
12
+//program to count no of bits
13
+int CountBits(int n)
14
+{
15
+ int count=0;
16
+ while(n)
17
+ {
18
+ n >>= 1; //simply right shift the no by 1
19
+ count++;
20
+ }
21
22
+ return count;
23
+}
24
25
26
+int CountSet(int n)
27
28
29
30
31
+ count += n & 1;
32
33
34
35
36
37
38
39
int main()
40
{
41
+ //cout<<CountSet(10);
42
+ int n = __builtin_popcount(7);
43
44
+ cout<<n;
45
return 0;
46
}
0 commit comments