-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_ones.cpp
89 lines (81 loc) · 2.37 KB
/
count_ones.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
/*
* COUNT SET BITS IN INTEGER
*
* This program demonstrates three modern C++ implementations to count the number
* of set bits (1s) in the binary representation of an integer. It includes:
*
* 1. Simple (Brute-force) Solution using Bit Masking:
* Iterates through every bit position using a shifting mask.
* Complexity: O(n) where n is the number of bits (fixed at 32 or 64).
*
* 2. Optimal (Efficient) Solution using Brian Kernighan's Algorithm:
* Repeatedly removes the lowest set bit until the number is zero.
* Complexity: O(k) where k is the number of set bits.
*
* 3. Additional Modern C++ Alternative using std::bitset:
* Leverages the standard library's bitset to count set bits.
*
* ASCII Illustration:
*
* Input number: 13 (binary: 1101)
* 13 = 1 1 0 1
* ↑ ↑
* set bits: 3 (three ones)
*
* Example Input/Output:
* Input: 13
* Output: 3
*
* Explanation:
* The binary representation of 13 is "1101", which contains three set bits.
*/
#include <algorithm>
#include <cassert>
#include <bitset>
#include <iostream>
#include <vector>
// Simple (Brute-force) Solution using Bit Masking
int countSetBitsSimple(int number) {
int count = 0;
for (unsigned int mask = 1; mask; mask <<= 1) {
if (number & mask) {
++count;
}
}
return count;
}
// Optimal (Efficient) Solution using Brian Kernighan's Algorithm
int countSetBitsOptimal(int number) {
int count = 0;
while (number) {
number &= (number - 1);
++count;
}
return count;
}
// Additional Modern C++ Alternative using std::bitset
int countSetBitsAlternative(int number) {
return static_cast<int>(std::bitset<32>(number).count());
}
// Test cases for correctness
void test() {
std::vector<std::pair<int, int>> testCases = {
{0, 0},
{1, 1},
{13, 3}, // 13 in binary: 1101
{255, 8}, // 255 in binary: 11111111
{0x7FFFFFFF, 31}
};
for (const auto &testCase : testCases) {
int input = testCase.first;
int expected = testCase.second;
assert(countSetBitsSimple(input) == expected);
assert(countSetBitsOptimal(input) == expected);
assert(countSetBitsAlternative(input) == expected);
}
std::cout << "All tests passed!\n";
}
int main() {
test();
return 0;
}