34B. Logarithms

x = log2(y) is defined to be the solution for x to equation y = 2x. For example, since 23 = 8, log2(8) = 3. Here are a few logarithms.

 x  log2(x)
16 4
32 5
64 6
128 7
256 8
512 9
1024 10
1,000,000 ~20
1,000,000,000 ~30

One way to compute an approximate logarithm of an integer x is to start with x and then perform steps where, at each step, you take half of the result of the previous step. But if you get a result that is not an integer, then round down. Stop when you reach 1. Then count the number of halving steps that you did. If you did h halving steps, then h is the largest integer that is ≤ log2(x). For example,

  1000
   500
   250
   125
    62
    31
    15
     7
     3
     1
involves 9 halving steps, so 9 ≤ log2(1000) ≤ 10. In fact, log2(1000) is just a little less than 10.

In this course, we write log(n) to mean log2(n).


Exercises

  1. What is log2(32)? Answer

  2. What is log2(128)? Answer

  3. What is the largest integer n such that n < log2(20)? Answer

  4. What is the largest integer n such that n < log2(150)? Answer