How many 19-bit strings can be generated from having an even number of ones?

make any string with the fist 18 digits. The last digit is forced to be a 1 or a 0, based on the number of 1s in the first 18.


There is no overlap in what you counted; if a string has four $1$s, then it cannot also have six $1$s. So you just need to take a sum over these possibilities.

A simpler way uses the following trick: if a string of length $19$ has an odd number of 1s, then its complement (replace all $0$s with $1$s and $1$s with $0$s) has an even number of ones, and vice versa. So take all possible binary strings and just divide by $2$.


I like the accepted solution but I just wanted to say something about doing the mathematics the "hard" way. You know that there are $\binom m k = \frac{m!}{k!(m-k)!}$ ways to choose a set of $k$ items from a set of $m$ items. Those items can be numbered bits chosen to be 1s or 0s. Of course you know the identity that: $$\sum_{k=0}^m \binom m k = 2^m$$ because of course if you sum up all the ways you might choose $k$ bits to be 1, then you get all possible bitstrings. However there is a simple proof of this which hinges on the idea that these coefficients appear in the binomial expansion, $$\sum_{k=0}^m \binom m k ~x^k ~y^{m-k}= (x + y)^m.$$ Just plug in $x = y = 1$ to find the above formula as a special case.

Well now you want to consider only even bitstrings, and so we can still "do it the hard way" by asking for a function of $k$ that is $1$ if $k$ is even or $0$ if $k$ is odd, and a great example is $[1 + (-1)^k]/2.$ Plugging that in, the sum that you want is just $$\sum_{k=0}^m \binom mk \frac{1 + (-1)^k}2 = \frac12 \sum_{k=0}^m \binom mk + \frac12 \sum_{k=0}^m \binom mk (-1)^k.$$ The first sum is clearly $2^m/2 = 2^{m-1}$ and the second sum we can use the above formula to reason that it is actually $(-1 + 1)^m/2 = 0^m/2 = 0.$

So you can do this the hard way if you'd prefer and you'll definitely get the same result.