title: Count Total Set Bits In All Numbers From 1 To N
signature: function count_total_set_bits_in_all_numbers_from_1_to_n(n)
test_code: using Printf
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  function validate_solution(actual, expected)
      return actual == expected
  end
  
  function driver(n, expected)
      try
  
          if validate_solution(PLACEHOLDER_FN_NAME(n), expected)
              return "PASSED";
          end
          return "FAILED";
      catch exception_obj
          return string(Base.typename(typeof(exception_obj)).wrapper);
      end
  end
  
  function main()
      
      result = driver(90, 279);
      @printf("TEST-0...%s\n",result);
      
      result = driver(56, 159);
      @printf("TEST-1...%s\n",result);
      
      result = driver(43, 112);
      @printf("TEST-2...%s\n",result);
      
      result = driver(31, 80);
      @printf("TEST-3...%s\n",result);
      
      result = driver(77, 231);
      @printf("TEST-4...%s\n",result);
      
      result = driver(35, 88);
      @printf("TEST-5...%s\n",result);
      
      result = driver(43, 112);
      @printf("TEST-6...%s\n",result);
      
      result = driver(66, 197);
      @printf("TEST-7...%s\n",result);
      
      result = driver(15, 32);
      @printf("TEST-8...%s\n",result);
      
      result = driver(95, 304);
      @printf("TEST-9...%s\n",result);
      
  end
  
  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;
  }