Prove the expectation of the number of trailing zeros

binaryprobability

Let $X$ be uniformly sampled from the integers $\{1, \dots, m\}$ for $m > 0$. For $x>0$, we define $f(x)$ to be the number of trailing zeros in the binary representation of $x$.

What is
$$
\mathbb{E}(f(X))\;?
$$

If $m$ goes to infinity it seems the limit is $1$. How would you prove that?


If $b = \lfloor \log_2 (m)\rfloor + 1$ is the number of bits in the binary representation of $m$ then it seems the answer is:

$$
\frac{\sum_{i=1}^b \left\lfloor \frac{m}{2^i} \right\rfloor}{m}
$$

But why is this true?

Best Answer

By definition, $$ \mathbb{E}(f(X))=\frac{\displaystyle\sum_{j=1}^mf(j)}{m}\ . $$ Now let $$ a_{ij}=\cases{0 & if the binary representation of $\ j$ \\ &has fewer than $\ i\ $ trailing zeroes\\ 1& otherwise.} $$ Then $$ f(j)=\sum_{i=1}^ba_{ij}\ , $$ and $$ \mathbb{E}(f(X))=\frac{\displaystyle\sum_{j=1}^m \sum_{i=1}^ba_{ij}}{m} $$ But the quantity $\ \left\lfloor\frac{m}{2^i}\right\rfloor\ $ is the number of integers in the set $\ \{1,2,\dots,m\}\ $ that are multiples of $\ 2^i\ $—that is, the number of such integers whose binary expansion has $\ i\ $ or more trailing zeroes. So $$ \left\lfloor\frac{m}{2^i}\right\rfloor=\sum_{j=1}^ma_{ij} , $$ and therefore \begin{align} \frac{\displaystyle\sum_{i=1}^b\left\lfloor\frac{m}{2^i}\right\rfloor}{m}&= \frac{\displaystyle\sum_{i=1}^b \sum_{j=1}^ma_{ij}}{m}\\ &=\frac{\displaystyle\sum_{j=1}^m \sum_{i=1}^ba_{ij}}{m}\\ &= \mathbb{E}(f(X))\ . \end{align}