4

Is there an equation that reflects how many values have a bit in position $i$ turned on for a list of values $0-N$?.

For example if $N=5$, our numbers are represented in binary as:

000,
001,
010,
011,
100,
101,

So in position 0 (least significant bit), there are 3 bits turned on, in position 1 there are 2 bits turned on, and in position 3 there are 2 bits turned on. Is there an equation that relates this for any position $i$ and value $N$?

TheoretiCAL
  • 185
  • 5

1 Answers1

1

You should easily observe that 'on average', each value is 0 50% or the time. In fact, there is a pattern to the position of the bits.

For non-negative integer $i$, the pattern is:

$2^i$ 0's followed by $2^i$ 1's.

Let $N = 2^{i+1} \times Q + R$, where $ 0 \leq R <2^{i+1}$. This tells us that out of the first $N$ numbers, the number of bits that are turned on in the $i$th position is ....

$2^i \times Q + \max(R- 2^i, 0)$.

Calvin Lin
  • 68,864