I want to find a closed form for $$\sum_{i=0,1,...}{\left\lfloor\frac{n}{2^i}\right\rfloor}$$
e.g.
1: 1
2: 2 + ⌊2/2⌋ = 3
3: 3 + ⌊3/2⌋ = 4
4: 4 + ⌊4/2⌋ + ⌊4/4⌋ = 7
5: 5 + ⌊5/2⌋ + ⌊5/4⌋ = 8
...
16: 16 + ⌊16/2⌋ + ⌊16/4⌋ + ⌊16/8⌋ + ⌊16/16⌋ = 31
For powers of 2 it is simply 2n-1, but for values in-between it is slightly smaller. Is there a closed form for this? How would I find it?
Assume that $2^{m-1}\le n<2^m$, so that the usual binary representation of $n$ has $m$ bits. Let that binary representation be $(b_mb_{m-1}\ldots b_1)_2$, where $b_m=1$, and note that
$$n=\sum_{k=1}^mb_k2^{k-1}\;.$$
Clearly $\left\lfloor\frac{n}{2^i}\right\rfloor=0$ when $i\ge m$, and $$\left\lfloor\frac{n}{2^i}\right\rfloor=(b_m\ldots b_{i+1})_2$$ when $0\le i<m$, so the number in question is
$$f(n)=\sum_{k=1}^m(b_m\ldots b_k)_2\;.\tag{1}$$
Fix $k\in\{1,\ldots,m\}$; $b_k$ appears in exactly $k$ terms of $(1)$, moving one place to the right in each additional division by $2$, so it contributes
$$b_k\sum_{i=0}^{k-1}2^i=b_k(2^k-1)$$
to the sum. Thus,
$$f(n)=\sum_{b_k=1}(2^k-1)\;.$$
Let $\beta(n)$ be the number of $1$ bits in $(b_m\ldots b_1)_2$; clearly
$$f(n)=\sum_{b_k=1}2^k-\beta(n)=2\sum_{k=1}^mb_k2^{k-1}-\beta(n)=2n-\beta(n)\;.$$