-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
33 lines (27 loc) · 1018 Bytes
/
Copy pathsolution.cpp
File metadata and controls
33 lines (27 loc) · 1018 Bytes
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
class Solution
{
public:
int countSetBits(int n)
{
long long total = 0; // to be safe for intermediate sums
// iterate over bit positions: 0, 1, 2, ... while (1<<i) <= n
for (int i = 0; (1LL << i) <= n; i++)
{
long long bitMask = 1LL << i; // 2^i
long long cycleLen = bitMask << 1; // 2^(i+1)
// number of complete cycles of length cycleLen
long long fullCycles = n / cycleLen;
// in each full cycle, bit i is '1' exactly bitMask times
total += fullCycles * bitMask;
// handle the remaining (incomplete) part
long long remainder = n % cycleLen;
// extra ones after the first bitMask zeros
long long extraOnes = remainder - bitMask + 1;
if (extraOnes > 0)
{
total += extraOnes;
}
}
return (int)total; // fits in int for given constraints
}
};