title: Count Total Set Bits In All Numbers From 1 To N
signature: countTotalSetBitsInAllNumbersFrom1ToN(n) {
test_code: import 'dart:convert';
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual.toString() == expected.toString();
  }
  
  String driver(n, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }catch(exception_object){
          return exception_object.runtimeType.toString();
      }
  }
  
  int main() {
  
      print("TEST-0...${driver(
          90, 
          279)
      }");
  
      print("TEST-1...${driver(
          56, 
          159)
      }");
  
      print("TEST-2...${driver(
          43, 
          112)
      }");
  
      print("TEST-3...${driver(
          31, 
          80)
      }");
  
      print("TEST-4...${driver(
          77, 
          231)
      }");
  
      print("TEST-5...${driver(
          35, 
          88)
      }");
  
      print("TEST-6...${driver(
          43, 
          112)
      }");
  
      print("TEST-7...${driver(
          66, 
          197)
      }");
  
      print("TEST-8...${driver(
          15, 
          32)
      }");
  
      print("TEST-9...${driver(
          95, 
          304)
      }");
  
      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;
  }