Artificial intelligent assistant

Average difference between two odd numbers of equal length If I select two different odd numbers of binary length $l$, what is the formula that will tell me the average difference between those two numbers? Note that the high order digit must always be $1$, so for example, qualifying numbers of length $4$ are from $9$ to $15$ inclusively, qualifying numbers of length $5$ are from $17$ to $31$ inclusively, etc. For length $l=3$, the answer is $2$ (the only possible numbers are $5$ and $7$). For length $l=4$, the answer is $3.3333...$ For length $l=5$, the answer is $6$. But I'd like to be able to calculate it for much larger lengths, and explore what happens as the length $l$ gets larger.

We try to simplify the set of all differences of odd $l$-digit binary numbers. Ignoring the first and last bits, because they are always $1$, we look at the differences of all numbers from the remaining $l-2$ digits, which is to say, numbers $1,2,\ldots,2^{l-2}$. Note, because we dropped the last digit, we must multiply our new result by $2$. Now, the sum, $S$, of all possible differences is

\begin{eqnarray*} S &=& 2\sum_{i=2}^{2^{l-2}} \sum_{j=1}^{i-1}{(i-j)} \\\ &=& 2\sum_{i=2}^{2^{l-2}} \sum_{j=1}^{i-1}{j} \\\ &=& \sum_{i=2}^{2^{l-2}}{(i^2 - i)} \\\ &=& \dfrac{1}{6} 2^{l-2} \left(2^{l-2}+1\right) \left(2^{l-1}+1\right) - \dfrac{1}{2} 2^{l-2} \left(2^{l-2}+1\right) \\\ \end{eqnarray*} using the Sum of Squares formula.

Now, the number, $N$ of these number-pairs, is

$$N=\binom{2^{l-2}}{2} = 2^{2l-5} - 2^{l-3}.$$

So the expected value is

$$\dfrac{S}{N} = \dfrac{2^{l-1}+2}{3}\qquad\text{after some tedious simplification.}$$

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 422bbff6e7da73f66ae30d4ae58b3d43