-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path04_count_set_bits.cpp
99 lines (72 loc) · 2.18 KB
/
04_count_set_bits.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Problem Name - Count Set Bits
count number of 1s in binary representation of an integer
Input Format: Input N = Number of Test Cases, followed by N numbers
Output Format: Number of Set Bits in each number each in a new line
Sample Input: 3
5
4
15
Sample Output: 2
1
4
Explanation: Convert Binary to Decimal first and then count number of 1's present in all digits.
*/
#include <iostream>
using namespace std;
// function to count set bits
int coutSetBits(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;
}
// function to drive code
int main()
{
int testcase, num;
cout << "Enter total testcases: ";
cin >> testcase;
while(testcase--)
{
cout << "Enter Number: ";
cin >> num;
cout << "Total Set Bits: ";
cout << coutSetBits(num) << endl;
}
return 0;
}
/*
OUTPUT:
Enter total testcases: 3
Enter Number: 5
Total Set Bits: 2
Enter Number: 4
Total Set Bits: 1
Enter Number: 15
Total Set Bits: 4
APPROACH:
With bitwise operations, we can use an algorithm whose running time depends on the
number of ones present in the binary form of the given number.
int count_set_nits (int n)
{
while( n > 0)
{
n = n&(n-1);
count++;
}
return count;
}
How does this algorithm work?
We can observe that there is a relationship between a number N and N-1.
N-1 will have all the bits same as x except for the rightmost 1 in x and all the bits to
the right of rightmost 1 in x.
So as in x-1, the rightmost 1 and bits right to it is flipped, then by performing x&(x-1),
and storing it in x, will reduce x to a number containing number of ones(in its binary form)
less than the previous state of x, thus increasing the value of count in each iteration.
This is also known as Brian Kernighan Algorithm.
*/