-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path07_counting_set_bits_n_optimisations.cpp
79 lines (61 loc) · 1.45 KB
/
07_counting_set_bits_n_optimisations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Topic: Counting Set Bits & Optimisations
- Given a Number N, find number of set bits in binary representation of it.
Eg: Input : 13 (1101)
Output: 3
*/
#include <iostream>
using namespace std;
// function to count set bits
int countBits(int n)
{
int count = 0;
while(n)
{
count += (n&1);
n = n>>1;
}
return count;
}
// approach 2
int countBitsFast(int n)
{
int count = 0;
while(n)
{
n = n & (n-1); // removing the last set bits
count++; // count is the no. of times loop run
}
return count;
/*
Eg: when n = 5 0101
n-1 = 4 & 0100
-------
n & (n-1) = 4 0100
when n = 4 0100
n-1 = 3 & 0011
-------
n & (n-1) = 0 0000
Time Complexity: O(No. of Set Bits)
*/
}
// function to drive code
int main()
{
int n;
cout << "Enter numbers: ";
cin >> n;
cout << "count: " << countBits(n) << endl; // Approach 1
cout << "count: " << countBitsFast(n) << endl; // Approach 2
cout << "count: " << __builtin_popcount(n) << endl; // Buildin Method
return 0;
}
/*
OUTPUT:
Case 1:
Enter numbers: 5
count: 2
Case 2:
Enter numbers: 13
count: 3
*/