How many $1$s are in the first $1023$ binary numbers?
I'm not to sure how to approach this question. An idea, formula, or solution is appreciated!
How many $1$s are in the first $1023$ binary numbers?
I'm not to sure how to approach this question. An idea, formula, or solution is appreciated!
Assuming the "first" binary number is $1$ note that the first $1023$ binary numbers, plus $0$, are all the binary numbers you can write with exactly $10$ binary digits or bits (prepending $0$s to "short" numbers, as in $0000101010_2$). Between all of them, you then have $1024 \cdot 10=10240$ bits, and for symmetry reasons exactly half of those, $5120$, are $1$s.
Some people define natural numbers as starting from $0$ (e.g. the ISO folks, https://en.wikipedia.org/wiki/ISO_31-11) others as starting from $1$ (e.g. Peano, the guy who first axiomatized the naturals in the 19th century, https://archive.org/details/arithmeticespri00peangoog). This is the essence of the confusion: because depending on whether you include the $0$ or not among the "first" $1023$ numbers, you exclude or don't exclude, respectively, $1023$ itself -- which carries exactly $10$ ones, yielding a total of 5120 vs. 5110 ones.
– Anonymous Feb 28 '17 at 07:52Hint: For how many of those numbers will the one's bit be a $1$ (in other words: how many of those numbers are odd)? For how many of them will the two's bit be a $1$? For how many of them will the four's bit be a $1$? And so on. Also, it will probably be advantageous to include $0$ (and thus look at a collection of $1024$ binary numbers) to make the counting a bit easier. Or, if $0$ is already included, include $1023$ initially, then correct for it when you're done counting.
Any such number can be represented by a string of 10 0s and 1s. The number of such strings with $n$ ones is $10$ choose $n$. Thus, the number of ones which appear is $$ \sum_{n=0}^{10} n{10 \choose n}=5\cdot 2^{10}. $$
As an addendum to Arthur's answer - if you count to, say, $2^{4} - 1 = 15$, you can easily figure out a pattern in the columns:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
So if you count to $2^{n} - 1$, you have $2^{n} * n$ digits ("rows * columns"), of which 50% are 1's.
For your example: Counting to $1023 =2^{10} - 1$, gets you $2^{10} * 10 * \frac{1}{2} = 5120$ binary 1's in total.
It's easy to calculate. There is a pattern with any number used as pow of tow:
You have 2^x, where x> 0 and you will have a binary number as:
2^0 = 1 (Dec) = 1 (Binary)
2^1 = 2 (Dec) = 10 (Binary)
2^2 = 4 (Dec) = 100 (Binary)
...
2^10 = 1024 (Dec) = 100 0000 0000(Binary)
If you pay attention you will notice this:
2^10 - 1 = 1023 (Dec) = 011 1111 1111(Binary)
...
2^2 - 1 = 3 (Dec) = 011 (Binary)
2^1 = 1(Dec) = 01 (Binary)
2^0 - 1 = 0 (Dec) = 0 (Binary)
In this case, your question, the Number of ones matches with the number that is used for pow.