0
$\begingroup$

What is the total running time of counting from 1 to n in binary if the time needed to add 1 to the current number i is proportional to the number of bits in the binary expansion of i that must change in going from i to i + 1?

Having trouble getting my head a around this one because I cannot find a formula for how bits change from some i to i+1. for example $$~~~~before~~~~~~after~~~~changes$$ $$0000 -> 0001 = 1$$ $$0001 -> 0010 = 2$$ $$0010 -> 0011 = 1$$ $$0011 -> 0100 = 3$$

  • 1
    Keep going. There ought to be some recognizable pattern. Counting from $2^n $ to $2^{n+1}-1$ will be the same as it takes to count from 0 to 2^n. But counting $2^n-1$ to $2^n $ will take n units.2017-01-18
  • 0
    And if you use a Gray code, the time is just $2^n$ since going from k to k+1 always involves changing only one bit.2017-01-18

2 Answers 2

1

The number of bit changes required to increment up to a number is equal to the number of trailing zeros after the last one. Say we are incrementing through an $n$-bit register which represents $2^n$ different numbers. Then we can count the total amount of numbers with $k$ trailing zeros after the final one.

For $k=0,$ this is easy: it is just the odd numbers so there are $2^{n-1}$ that require one bit flip. For $k=1,$ the last two bits are fixed to $1$ and $0$, while the others are free to be whatever, so there are $2^{n-2}$ possibilities. That's the pattern. For general $k$ there are $2^{n-k-1}$ different numbers that have $k$ trailing zeros after the last one. Since there are $k+1$ bit flips for a number with $k$ trailing zeros we have $$ N = \sum_{k=0}^{n-1}(k+1)2^{n-k-1} = 2^{n+1}-n-2$$

for counting from $0$ to $2^n-1.$

  • 0
    how can this formula be correct? Suppose n = 16, then you have $$2^{17} - 16 - 2$$ which is way too many for n = 16, the number of bit changes is 26 for n= 16 or does N reprsent the time it takes? Your method kind of makes sense to me, but when you say, the number of zeros after the 'last one' what do you mean?2017-01-18
  • 1
    The $n$ in the formula is for when you are counting from $0$ to $2^n-1.$ So if you count from $0$ to $2^{16}-1$ you will have $2^{17}-16-2$ bit flips. Try it for $n=4,$ i.e. counting from $0$ to $15,$ or $0000$ to $1111.$ You should get $32-4-2 = 26$ bit flips.2017-01-18
  • 1
    By the 'number of zeros after the last one' I mean the number of trailing zeros after the final occurrence of (the digit) 1. So for 101001000 The number of trailing zeros is three. Those last three zeros in the number after the final 1. Incrementing to this number from the number before it will cause $4$ bits to flip.2017-01-18
  • 0
    i see, so does this mean it only works if n is a power of 2?2017-01-18
  • 0
    My $n$ or your $n$? My formula holds only for when you're counting from zero to one less than a power of two.2017-01-18
  • 0
    your formula, yes okay thats what i thought. I think I need to have it work for any integer n. but nonetheless your response has provided me lots of useful insights about the problem2017-01-18
1

HINT Assuming $n=2^k$ , note that the last bit changes $2^k-1$ times. How many times do the other bits change? (the pattern may become more clear if you first go from $0$ to $n-1$). If you sum up all these, you get the same sum you are looking for.