title: Count Total Set Bits In All Numbers From 1 To N
signature: countTotalSetBitsInAllNumbersFrom1ToN :: Integer -> Integer
  countTotalSetBitsInAllNumbersFrom1ToN n = 
test_code: import Text.Printf
  import Control.Exception (catch, SomeException)
  import Data.Typeable
  import qualified Data.List as List
  import qualified Data.Map as Map
  import qualified Data.Set as Set
  
  -- SOLUTION CODE
  -- ============================================
  
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  validateSolution :: Integer -> Integer -> Bool
  validateSolution actual expected = do
      actual == expected
  
  
  driver :: Integer -> Integer -> String
  driver n expected = do
      if (validateSolution (PLACEHOLDER_FN_NAME n) expected)
          then "PASSED"
      else "FAILED"
  
  main = do
      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) )
  
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;
  }