10

I'm taking a Computer Algorithms class and one of my problems is from Skiena's Algorithm Design Manual, 2-41:

Prove that the binary representation of $n \ge 1$ has $\lfloor \lg n \rfloor +1$ bits ($\lg$ is base 2)

Some base cases:

$n = 1, \lfloor \lg 1 \rfloor + 1 = 1$
$n = 2, \lfloor \lg 2 \rfloor + 1 = 2$
$n = 5, \lfloor \lg 5 \rfloor + 1 = 3$
$n = 15, \lfloor \lg 15 \rfloor + 1 = 4$

I don't know where to go from there though. Any help appreciated.

Bill Dubuque
  • 282,220
  • I'd add some more examples. To represent 3 we need 2 bits: 11. To represent 4-7 we need 3 bits: 100-111. To represent 8 we need 4 bits: 1000. We may see that $\lfloor log_2(3) \rfloor + 1 = 2$, $\lfloor log_2(4) \rfloor + 1 = 3$ and so on. – irudyak Apr 14 '23 at 08:41

3 Answers3

6

Let $n \in \mathbb{N}$, suppose $n$ requires $d$ digits in it's base 2 representation, we'd like to show that $d = \lfloor \log_2(n) \rfloor+1$.

At most we have $n = 1 \ldots 1$, that is $d$ 1's in a row. But we know that is $$\sum_{i=0}^{d-1} 2^i = 2^d - 1$$

At a minimum the first digit is a 1 and the rest are zeros (since the powers start at 0 right to left, this is $2^{d - 1}$).

We now have the following bound on $n$. $$2^{d-1 } \le n \le 2^d - 1$$

Taking log base 2 on on the above inequality yields: (Call this inequality $\beta$) $$d - 1 \le \log_2 (n) \le \log_2(2^d -1 )$$

Note: Taking the log respects the inequalities because $\log_2(\cdot)$ is a strictly increasing function (check it's derivative)

We will attempt to take the floor of $\beta$. Since $d-1$ is an integer $\lfloor d-1 \rfloor = d-1$, as for $\log_2(2^d-1)$, we must look a little closer. Since $2^{d-1} \le 2^d - 1 < 2^d$ and , we know that $$d-1 = \log_2(2^{d-1}) \le \log_2(2^d - 1) < log_2(2^d) = d $$

Therefore $\lfloor \log_2(2^d - 1) \rfloor = d-1$ and so the result of taking the floor of $\beta$ yields

$$d - 1 \le \lfloor \log_2 (n) \rfloor \le \lfloor \log_2(2^d -1 ) \rfloor = d-1$$

In other words

$$d - 1 \le \lfloor \log_2 (n) \rfloor \le d-1$$

So $$d - 1 = \lfloor \log_2 (n) \rfloor \Leftrightarrow d = \lfloor \log_2 (n) \rfloor + 1$$

  • Sorry I'm confused are you applying the floor function to the inequality here? – Bob Marley Jul 15 '24 at 04:55
  • @BobMarley Can you specify what step you're confused about? – cuppajoeman Jul 16 '24 at 04:16
  • When you say you're taking the floor of the $\beta$ inequality, you're applying the floor function to it? I'm just not used to seeing the floor function being applied to an inequality. – Bob Marley Jul 18 '24 at 12:32
  • 1
    @BobMarley Ok, I see what you're saying. Yes I'm applying the floor function to everything, because if you know that $a \le b$ for any real numbers then $\lfloor a \rfloor \le \lfloor b \rfloor$ – cuppajoeman Jul 18 '24 at 16:36
2

Hint: For which numbers $n$ is $\lg n$ an integer?

Harald Hanche-Olsen
  • 32,608
  • 2
  • 61
  • 87
1

Hint
Note that the binary representation of $2^n$ has $n+1$ bits.
Find then that the binary representation of a sum of $2^{k_i}$ with distinct $k_i$ has $\max_i k_i + 1$ bits.
Finally conclude that any integer in the interval $[2^n, 2^{n+1})$ has a binary representation of exactly $n+1$ bits.

AlexR
  • 25,110