Set bits 0 to n
Your task
Given a non-negative integer n (0 <= n <= 10^9), return the total number of set bits (1s) across the binary representations of every integer from 0 to n inclusive. For example for n = 5 the values 0..5 in binary contribute 0+1+1+2+1+2 = 7 set bits. An O(n) loop popcounting each number is too slow at the upper bound.
Examples
count_set_bits_upto(n: int) -> intExample 1
Input
count_set_bits_upto(5)Output
7