6

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!

miracle173
  • 11,049
user406996
  • 663
  • 1
  • 5
  • 14

5 Answers5

21

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.

Anonymous
  • 5,789
  • why 1024*10 digits? – user406996 Feb 26 '17 at 00:28
  • 2
    Ohh, the amount of 0's don't matter! I see! – user406996 Feb 26 '17 at 00:29
  • Thanks! I'll keep this solution in mind from now on! You can just add the 0's to make the problem simpler! – user406996 Feb 26 '17 at 00:29
  • But the assumption is wrong. The first is 0. So, we must subtract 10 to this answer. – Dominic108 Feb 26 '17 at 01:31
  • @Dominic108 0 has no 1 digits in it so this doesn't matter. – user253751 Feb 26 '17 at 02:12
  • 3
    It does matter, because if we start at 0, then 1111111111 should not be counted among the the first 1023 binary numbers. The question did not say the first 1023 positive binary numbers. – Dominic108 Feb 26 '17 at 02:23
  • @Dominic108 "The question did not say the first 1023 positive binary numbers" - good point! If we assume numerical order then there are no 1023 lowest numbers. – user253751 Feb 26 '17 at 02:31
  • 1
    In computer science, typically indexing start at 0. I was biased . I should have read the comments below the question... The issue was raised before. – Dominic108 Feb 26 '17 at 02:52
  • How come this symmetry claim is true? Under what circumstances would it be true? – Mario Ishac Feb 26 '17 at 05:26
  • 1
    @MarDev We're looking at all of the integers we can represent with 10 bits - for every integer there is exactly one other which has all of its bits inverted (the result of an XOR with 1111111111) and therefore the opposite amount of zeros and ones. Furthermore, this inversion always lies in the opposite half of the possible numbers (the operation is essentially equivalent to 1111111111 - n) so each of the first 2^9 numbers (half of the possible 2^10) there exists exactly one number in the second half with the opposite number of zeros and ones. – acernine Feb 26 '17 at 08:46
  • It seems correct: [`sum(bin(n).count("1") for n in range(1023)) -> 5110](http://ideone.com/oIEda4) (starting from zero) – jfs Feb 26 '17 at 09:29
  • @MarDev: For every number you can find a number that you can get by replacing all 0s with 1s and all 1s with 0s ($f(n) = 1023-n$). This function is a bijection and the number of 1s in the original value and the output of that function is 10. Using both original value and output of the function you've counted the 1s in each number twice (once as original value and once as output) which means you need to divide by 2... – fabian Feb 26 '17 at 11:26
  • I guess I should have been more clear in the question. I meant for it to be interpreted this way: Take the first 1023 natural numbers (Base 10) Turn them to base 2. How many 1's are there? – user406996 Feb 28 '17 at 00:18
  • @user406996 This provides no clarification at all :)

    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:52
  • For a nice discussion about the lack of consensus on whether natural numbers start from 0 or 1 see: http://math.stackexchange.com/questions/283/is-0-a-natural-number – Anonymous Feb 28 '17 at 08:04
9

Hint: 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.

Arthur
  • 199,419
  • I'm struggling with that idea right now. I'm not entirely sure how to solve that. Is it like half half? So if there are x digits in total, half of them are 1? – user406996 Feb 26 '17 at 00:27
  • On average, yes, half of the bits will be a $1$. Or something. Depends on what you mean by "of the bits". – Arthur Feb 26 '17 at 00:31
8

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}. $$

TorsionSquid
  • 3,530
4

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.

phoibos
  • 141
1

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.