title: Count Total Set Bits In All Numbers From 1 To N
signature: function countTotalSetBitsInAllNumbersFrom1ToN($n) {
test_code: <?php
  
  
  // SOLUTION CODE 
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function validateSolution($actual, $expected){
      return serialize($actual) == serialize($expected);
  }
  
  function driver($n, $expected){
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME($n), $expected)){
          return "PASSED";
        }
        return "FAILED";
      }
      catch (Exception $exception_obj) {
        return get_class($exception_obj);
      }
  }
  
  function main(){
      printf("TEST-0...%s\n",driver(
        90, 
        279));
      
      printf("TEST-1...%s\n",driver(
        56, 
        159));
      
      printf("TEST-2...%s\n",driver(
        43, 
        112));
      
      printf("TEST-3...%s\n",driver(
        31, 
        80));
      
      printf("TEST-4...%s\n",driver(
        77, 
        231));
      
      printf("TEST-5...%s\n",driver(
        35, 
        88));
      
      printf("TEST-6...%s\n",driver(
        43, 
        112));
      
      printf("TEST-7...%s\n",driver(
        66, 
        197));
      
      printf("TEST-8...%s\n",driver(
        15, 
        32));
      
      printf("TEST-9...%s\n",driver(
        95, 
        304));
      
  }
  
  main();
  ?>
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;
  }