aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/ash/cpp/ch-1.cpp
blob: 27596b89670753c43afe8713b301437aa0f067c9 (plain)
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
/*
    Task 1 from
    https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/
*/

#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char** argv) {
    if (argc != 2) {
        cerr << "Usage: ./a.out 42" << endl;
        return 1;
    }

    stringstream input(argv[1]);
  
    int n;
    input >> n;

    int total_bits = 0;
    int scale = 1;
    n++;
    while (scale <= n) {
        int scale2 = scale << 1;
        
        int fill_full = n / scale2;
        int fill_frac = n % scale2;

        int bits_full = fill_full * scale;
        int bits_frac = 0;
        if (fill_frac > scale) {
            bits_frac = fill_frac - scale;
        }

        total_bits += bits_full + bits_frac;

        scale = scale2;
    }

    cout << "There are " << total_bits << " set bits in the sequence from 1 to " << n << endl;
}