title: Count Total Set Bits In All Numbers From 1 To N
signature: int countTotalSetBitsInAllNumbersFrom1ToN(int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(int n, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver(90, 279); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver(56, 159); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver(43, 112); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver(31, 80); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver(77, 231); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver(35, 88); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver(43, 112); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver(66, 197); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver(15, 32); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver(95, 304); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def count_total_set_bits_in_all_numbers_from_1_to_n(n):
      i = 0
      ans = 0
      while ((1 << i) <= n):
          k = 0
          change = (1 << i)
          for j in range(0, (n + 1)):
              ans += k
              if (change == 1):
                  k = (not k)
                  change = (1 << i)
              else:
                  change -= 1
          i += 1
      return ans
source_cpp: int count_total_set_bits_in_all_numbers_from_1_to_n(int n) {
    int i = 0;
    int ans = 0;
    while ( ( 1 << i ) <= n ) {
      bool k = 0;
      int change = 1 << i;
      for ( int j = 0;
      j <= n;
      j ++ ) {
        ans += k;
        if ( change == 1 ) {
          k = ! k;
          change = 1 << i;
        }
        else {
          change --;
        }
      }
      i ++;
    }
    return ans;
  }