title: Count Total Set Bits In All Numbers From 1 To N
signature: pub fn count_total_set_bits_in_all_numbers_from_1_to_n(n: i32) -> i32 {
test_code: use std::collections::HashMap;
  use std::collections::HashSet;
  use std::cmp::*;
  use std::collections::*;
  use std::fmt::*; 
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  pub fn validate_solution(actual: i32, expected: i32)->bool{
  return actual==expected
  }
  pub fn driver(n: i32, expected: i32)->String{
      if validate_solution(PLACEHOLDER_FN_NAME(n), expected) {
        return "PASSED".to_string();
      };
      return "FAILED".to_string();
  
  }
  
  fn main() {
      println!("TEST-0...{}", driver(
        90, 
        279
      ));
      
      println!("TEST-1...{}", driver(
        56, 
        159
      ));
      
      println!("TEST-2...{}", driver(
        43, 
        112
      ));
      
      println!("TEST-3...{}", driver(
        31, 
        80
      ));
      
      println!("TEST-4...{}", driver(
        77, 
        231
      ));
      
      println!("TEST-5...{}", driver(
        35, 
        88
      ));
      
      println!("TEST-6...{}", driver(
        43, 
        112
      ));
      
      println!("TEST-7...{}", driver(
        66, 
        197
      ));
      
      println!("TEST-8...{}", driver(
        15, 
        32
      ));
      
      println!("TEST-9...{}", driver(
        95, 
        304
      ));
      
  }
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;
  }