6
$\begingroup$

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!

  • 4
    Have you noticed that $1023 = 2^{10}-1$?2017-02-26
  • 6
    What is the "first" binary number? 0 or 1?2017-02-26
  • 0
    Worth clarifying that we are talking about positive, unsigned integers.2017-02-26
  • 0
    @J... actually, can an unsigned integer be positive?2017-02-26
  • 0
    @Joffan Cheeky bastard :P Yes, unsigned integers are positive because all of them (other than 0) are greater than 0.2017-02-26
  • 0
    If you did not immediately have an idea, you should first have solved it for 1, 3, 7, 15 to see if you notice a pattern. (That is, of course, if you noticed why 1023 was chosen.)2017-02-26

5 Answers 5

20

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.

  • 0
    why 1024*10 digits?2017-02-26
  • 2
    Ohh, the amount of 0's don't matter! I see!2017-02-26
  • 0
    Thanks! I'll keep this solution in mind from now on! You can just add the 0's to make the problem simpler!2017-02-26
  • 0
    But the assumption is wrong. The first is 0. So, we must subtract 10 to this answer.2017-02-26
  • 0
    @Dominic108 0 has no 1 digits in it so this doesn't matter.2017-02-26
  • 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.2017-02-26
  • 0
    @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.2017-02-26
  • 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.2017-02-26
  • 0
    How come this symmetry claim is true? Under what circumstances would it be true?2017-02-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.2017-02-26
  • 0
    It seems correct: [`sum(bin(n).count("1") for n in range(1023)) -> 5110](http://ideone.com/oIEda4) (starting from zero)2017-02-26
  • 0
    @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...2017-02-26
  • 0
    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?2017-02-28
  • 0
    @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.2017-02-28
  • 0
    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-number2017-02-28
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.

  • 0
    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?2017-02-26
  • 0
    On average, yes, half of the bits will be a $1$. Or something. Depends on what you mean by "of the bits".2017-02-26
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}. $$

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.

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.