Expected number and probability of a series of 1s in a bit string. Suppose the sequence 1001111011011 has a total of $4$ "blocks" of one, because number of contiguous sequences made of ones (1, 1111,11 and 11) count to 4. Given a random bit array of length $N$, what's the expected value of the "number of 1 blocks"? Related to that, what is the probability there are $M$ blocks in a bit a array of length $N$?

Using $z$  for zeros  and $w$  for ones and  $u$ for  runs we  get the
generating function
$$(1+z+z^2+\cdots)
\\ \times \sum_{q\ge 0} u^{q} (w+w^2+\cdots)^q (z+z^2+\cdots)^q
\\ \times (1+uw+uw^2+\cdots)$$
which is
$$\frac{1}{1-z}
\\ \times \sum_{q\ge 0} u^{q} \frac{w^q}{(1-w)^q} \frac{z^q}{(1-z)^q}
\\ \times \left(1+u\frac{w}{1-w}\right)$$
or
$$\frac{1}{1-z}
\frac{1}{1-uwz/(1-z)/(1-w)}
\frac{1-w+uw}{1-w}
\\ = \frac{1-w+uw}{(1-z)(1-w)-uwz}.$$
As a sanity check we put $u=1$ and $w=z$ to obtain
$$\frac{1}{(1-z)^2 - z^2} = \frac{1}{1-2z}$$
and we see that we have accounted for all strings of length $N.$ We no
longer need the distinction between zeros and ones here so we obtain
$$\frac{1-z+uz}{(1-z)^2-uz^2}
= \frac{1-z+uz}{(1-z)^2}\frac{1}{1-uz^2/(1-z)^2}
\\ = \left(\frac{1}{1-z} + \frac{uz}{(1-z)^2}\right)
\frac{1}{1-uz^2/(1-z)^2}.$$
Extract the coefficient on $[u^M]$ to get
$$\frac{z^{2M}}{(1-z)^{2M+1}}
+ \frac{z^{2M-1}}{(1-z)^{2M}}
\\ =  \left(\frac{z}{1-z} + 1\right) \frac{z^{2M-1}}{(1-z)^{2M}}
= \frac{z^{2M-1}}{(1-z)^{2M+1}}.$$
Continue with the coefficient on $[z^N]$ to obtain
$$[z^N] \frac{z^{2M-1}}{(1-z)^{2M+1}}
= [z^{N+1-2M}] \frac{1}{(1-z)^{2M+1}}
\\ = {N+1-2M+2M\choose 2M} = {N+1\choose 2M}$$
for a probability of
$$\bbox[5px,border:2px solid #00A000]{
\frac{1}{2^N} {N+1\choose 2M}.}$$
We get for the expectation
$$\sum_{M=0, \; 2M\le N+1} M {N+1\choose 2M}
= \sum_{M=1, \; 2M\le N+1} M {N+1\choose 2M}
\\ = \frac{N+1}{2} \sum_{M=1, \; 2M\le N+1} {N\choose 2M-1}
= \frac{N+1}{2} \sum_{q=0}^N {N\choose q} \frac{1-(-1)^q}{2}
\\ = \frac{N+1}{4} 2^N.$$
Dividing by $2^N$ then yields
$$\bbox[5px,border:2px solid #00A000]{
\frac{N+1}{4}.}$$
This may also be obtained by linearity of expectation. We place a zero
value at the  front of the string  and may then count runs  of ones by
the number  of times  a zero  was followed  by a  one in  the modified
string, which  gives $1/2  + (N-1)/4,$ the  same answer  ($N-1$ places
where we transition from one bit to the next).
