Skip to content

halving and logarithms

the maths you need

what it is

Halve a number until 1 is left. The count of halvings is its log to base 2, rounded down or up.

16 is 2 × 2 × 2 × 2, 2 to the power 4. Halving leaves 8, 4, 2 and 1, one 2 fewer each time: four halvings. The power, 4, is its log to base 2, written log⁡2\log_2. Half of 11 is 5.5, not whole, so a halving rounds down or up.

halvings811, rounding down11, rounding up16
08111116
14568
22234
31122
411
Rounding down, 11 reaches 1 after 3 halvings, as 8 does; rounding up, after 4, as 16 does.
rounding down115.5 → 52.5 → 213 halvingsrounding up115.5 → 631.5 → 214 halvings
Dashed, the exact half; red, cut off (hollow) or added (solid); blue, 1 reached.

For powers not whole, this page takes two rules on trust. Multiplying powers of 2 adds the powers: 4 × 8 is 2 to the power 2 + 3. And 2 raised to a bigger power gives a bigger number. So a bigger number has a bigger log: if 11's log were 3 or less, 11 would be 8 or less, and so for any two numbers. 11 lies between 8 and 16, so its log lies between 3 and 4: about 3.46 by calculator, as 2 to the power 3.46 is about 11.

numberlog× 2+ 110214283164113.46
11 lies between 8 and 16, its log between 3 and 4.

For a number nn, rounding down is written with floor brackets, ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor, and rounding up with ceiling brackets, ⌈log⁡2 n⌉\lceil \log_2\, n \rceil. For 11 they are 3 and 4; for 16, both 4.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1Halve 2n2^n again and again. How many halvings reach 1?
  1. the move: name the log

    the line:
    L=log⁡2(2n)L = \log_2 (2^n)

    why it is allowed: Here nn counts 2s; the number halved is 2n2^n, read 2 to the power nn: nn 2s multiplied together. For 16, nn is 4.

  2. the move: its log is its count of 2s

    the line:
    L=nL = n

    why it is allowed: By what a log is. 16 is four 2s, so its log is 4.

  3. the move: count the halvings

    the line:
    2n comes down to 1in exactly n halvings\begin{gathered} 2^n \text{ comes down to 1} \\ \text{in exactly } n \text{ halvings} \end{gathered}

    why it is allowed: Each halving takes away one 2, so no fraction appears. After nn halvings the number is 1.

  4. the move: read it

    the line:
    a power of two halves to 1in as many halvings as its log\begin{gathered} \text{a power of two halves to 1} \\ \text{in as many halvings as its log} \end{gathered}

    why it is allowed: By lines 2 and 3. For 16, four of each.

try it with a small nn: at n=4n = 4, line 1 gives L=4L = 4, which you can work out by hand

question 2Halve nn, rounding down each time. How many halvings reach 1?
  1. the move: name the log rounded down

    the line:
    K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
    numberlog× 2+ 14283164113.46352.322211100
    Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving.

    why it is allowed: Here nn is the number halved, from 2 up; for 11, KK is 3.

  2. the move: rounding down never adds

    the line:
    ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

    why it is allowed: For 11, 3 is at most 3.46.

  3. the move: rounding down takes off less than 1

    the line:
     log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

    why it is allowed: For 11, 4 is above 3.46.

  4. the move: raise 2 to each side of line 2

    the line:
    2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

    why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

  5. the move: 2 to the log of n is n

    the line:
    2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

    why it is allowed: By what a log is. For 11, 8 is at most 11.

  6. the move: raise 2 to each side of line 3

    the line:
    2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

    why it is allowed: As in line 4. For 11, about 11 is below 16.

  7. the move: 2 to the log of n is n

    the line:
    n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

    why it is allowed: As in line 5. For 11, 11 is below 16.

  8. the move: halve both sides of line 5

    the line:
    2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

    why it is allowed: For 11, 4 is at most 5.5, which is not whole.

  9. the move: write the half as a power

    the line:
    2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

    why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

  10. the move: round the half down

    the line:
    2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

    why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as KK is at least 1 for nn from 2 up.

  11. the move: halve both sides of line 7

    the line:
    n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

    why it is allowed: For 11, 5.5 is below 8.

  12. the move: write the half as a power

    the line:
    n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

    why it is allowed: As in line 9. Half of 16 is 8.

  13. the move: round the half down

    the line:
    ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

    why it is allowed: Rounding down never adds. For 11, 5 is below 8.

  14. the move: take the log of line 10

    the line:
     log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

    why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

  15. the move: the log of a power of 2 is the power

    the line:
    ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

    why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

  16. the move: take the log of line 13

    the line:
     log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

    why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

  17. the move: the log of a power of 2 is the power

    the line:
     log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

    why it is allowed: For 11, about 2.32 is below 3.

  18. the move: round the log down

    the line:
    ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
    the numberat leastbelowits log rounded down
    118163
    5482
    2241
    1120
    Each halving takes 1 off the log rounded down.

    why it is allowed: By lines 15 and 17, the log of the half is at least one less than KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

  19. the move: use line 18 again and again

    the line:
    after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

    why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after KK. For 11, 3, 2, 1, 0.

  20. the move: a log rounded down to 0 belongs to 1

    the line:
    K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

    why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.

try it with a small nn: at n=11n = 11, line 1 gives K=3K = 3, which you can work out by hand

question 3Binary search on a row of nn numbers. At most how many looks?
  1. the move: what one look keeps

    the line:
    at most half, rounded down,of what is left\begin{gathered} \text{at most half, rounded down,} \\ \text{of what is left} \end{gathered}
    two middleslooking for 7510203040506070803 beforedropped4 afterkeptthe look, in neither sidethe other middle
    8 numbers have two middles; the table's 7 have one.
    lookstill to searchhalf, rounded downthe middlekept
    110, 20, 30, 40, 50, 60, 703.5 to 34050, 60, 70
    250, 60, 701.5 to 16070
    3700.5 to 070none
    75 is above all 7 numbers, so each look keeps the side after.

    why it is allowed: This site's binary search looks at the middle of what is left, the first of two middles, and keeps the side the wanted number must be on. From 8 it keeps 3 before or 4 after: the side after holds half, rounded down, the side before no more.

  2. the move: fewer numbers, no more looks

    the line:
    keeping a smaller part nevercosts more looks\begin{gathered} \text{keeping a smaller part never} \\ \text{costs more looks} \end{gathered}
    after lookkeeping the bigger sidekeeping the smaller side
    01010
    154
    221
    31none
    4none
    Two searches on 10 numbers. Keeping the bigger side runs out after 4 looks, the smaller after 3.

    why it is allowed: Let A always keep the bigger side, and S be any search on the same row. While S holds no more than A, it keeps at most half of its part, rounded down, so no more than A keeps. In the table, S keeps 4 to A's 5, then 1 to 2. So S runs out no later.

  3. the move: a number that keeps the bigger side

    the line:
    a number above them allkeeps the bigger side\begin{gathered} \text{a number above them all} \\ \text{keeps the bigger side} \end{gathered}

    why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.

  4. the move: count that search's looks

    the line:
    ⌊log⁡2 n⌋ looks leaveone number, one morelook leaves none\begin{gathered} \lfloor \log_2\, n \rfloor \text{ looks leave} \\ \text{one number, one more} \\ \text{look leaves none} \end{gathered}

    why it is allowed: Each look halves, rounding down, so question 2 counts the looks to one number. 1 number takes no halving, log 0. For 7, 7 leaves 3, 3 leaves 1, and look 3 leaves none.

  5. the move: the most looks

    the line:
    B=⌊log⁡2 n⌋+1B = \lfloor \log_2\, n \rfloor + 1

    why it is allowed: Let BB be the most looks, which lines 2 to 4 count. For 7 numbers, the search for 75 takes 3, as the table at line 1 shows.

  6. the move: read it

    the line:
    one more look thanthe log rounded down\begin{gathered} \text{one more look than} \\ \text{the log rounded down} \end{gathered}

    why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.

try it with a small nn: at n=7n = 7, line 5 gives B=3B = 3, which you can work out by hand

question 4Halve nn, rounding up each time. How many halvings reach 1?
  1. the move: name the log rounded up

    the line:
    C=⌈log⁡2 n⌉C = \lceil \log_2\, n \rceil

    why it is allowed: Here nn is the number halved, from 2 up. For 11, CC is 4.

  2. the move: rounding up never takes off

    the line:
    log⁡2 n≤⌈log⁡2 n⌉\log_2\, n \le \lceil \log_2\, n \rceil

    why it is allowed: The bracket is CC. For 11, 3.46 is at most 4.

  3. the move: rounding up adds less than 1

    the line:
    ⌈log⁡2 n⌉−1<log⁡2 n\lceil \log_2\, n \rceil - 1 < \log_2\, n

    why it is allowed: For 11, 3 is below 3.46.

  4. the move: raise 2 to each side of line 2

    the line:
    2log⁡2 n≤2⌈log⁡2 n⌉2^{\log_2\, n} \le 2^{\lceil \log_2\, n \rceil}

    why it is allowed: As in line 4 of question 2.

  5. the move: 2 to the log of n is n

    the line:
    n≤2⌈log⁡2 n⌉n \le 2^{\lceil \log_2\, n \rceil}

    why it is allowed: As in line 5 of question 2. For 11, 11 is at most 16.

  6. the move: raise 2 to each side of line 3

    the line:
    2⌈log⁡2 n⌉−1<2log⁡2 n2^{\lceil \log_2\, n \rceil - 1} < 2^{\log_2\, n}

    why it is allowed: As in line 4.

  7. the move: 2 to the log of n is n

    the line:
    2⌈log⁡2 n⌉−1<n2^{\lceil \log_2\, n \rceil - 1} < n

    why it is allowed: As in line 5. For 11, 8 is below 11.

  8. the move: halve both sides of line 5

    the line:
    n/2≤2⌈log⁡2 n⌉/2n / 2 \le 2^{\lceil \log_2\, n \rceil} / 2

    why it is allowed: Halving both sides keeps it true.

  9. the move: write the half as a power

    the line:
    n/2≤2⌈log⁡2 n⌉−1n / 2 \le 2^{\lceil \log_2\, n \rceil - 1}

    why it is allowed: As in line 9 of question 2. For 11, 5.5 is at most 8.

  10. the move: round the half up

    the line:
    ⌈n/2⌉≤2⌈log⁡2 n⌉−1\lceil n / 2 \rceil \le 2^{\lceil \log_2\, n \rceil - 1}

    why it is allowed: ⌈n/2⌉\lceil n / 2 \rceil is the half rounded up: the smallest whole number at or above it, 6 for 11. The right side, 8 for 11, is whole, as CC is at least 1. By line 9 it is at or above the half, so no smaller than the half rounded up.

  11. the move: halve both sides of line 7

    the line:
    2⌈log⁡2 n⌉−1/2<n/22^{\lceil \log_2\, n \rceil - 1} / 2 < n / 2

    why it is allowed: Halving both sides keeps it true.

  12. the move: write the half as a power

    the line:
    2⌈log⁡2 n⌉−2<n/22^{\lceil \log_2\, n \rceil - 2} < n / 2
    numberlog× 2+ 11/2−1102142
    One half, a step left of 1, is 2 to the power minus 1.

    why it is allowed: As in line 9 of question 2. For 11, 4 is below 5.5. For 2, CC is 1, so this is 2 to the power minus 1, one half, as drawn.

  13. the move: round the half up

    the line:
    2⌈log⁡2 n⌉−2<⌈n/2⌉2^{\lceil \log_2\, n \rceil - 2} < \lceil n / 2 \rceil

    why it is allowed: Rounding up never takes off. For 11, 4 is below 6.

  14. the move: take the log of line 10

    the line:
    log⁡2 ⌈n/2⌉≤log⁡2 2⌈log⁡2 n⌉−1\log_2\, \lceil n / 2 \rceil \le \log_2\, 2^{\lceil \log_2\, n \rceil - 1}

    why it is allowed: As in line 14 of question 2.

  15. the move: the log of a power of 2 is the power

    the line:
    log⁡2 ⌈n/2⌉≤⌈log⁡2 n⌉−1\log_2\, \lceil n / 2 \rceil \le \lceil \log_2\, n \rceil - 1

    why it is allowed: For 11, the log of 6, about 2.58, is at most 3.

  16. the move: take the log of line 13

    the line:
    log⁡2 2⌈log⁡2 n⌉−2<log⁡2 ⌈n/2⌉\log_2\, 2^{\lceil \log_2\, n \rceil - 2} < \log_2\, \lceil n / 2 \rceil

    why it is allowed: As in line 14. For 2, the log of one half is minus 1, below the log of 1, which is 0.

  17. the move: the log of a power of 2 is the power

    the line:
    ⌈log⁡2 n⌉−2<log⁡2 ⌈n/2⌉\lceil \log_2\, n \rceil - 2 < \log_2\, \lceil n / 2 \rceil

    why it is allowed: For 11, 2 is below about 2.58.

  18. the move: round the log up

    the line:
    ⌈log⁡2 ⌈n/2⌉⌉=⌈log⁡2 n⌉−1\lceil \log_2\, \lceil n / 2 \rceil \rceil = \lceil \log_2\, n \rceil - 1
    the numberaboveat mostits log rounded up
    118164
    6483
    3242
    2121
    11/210
    Each halving takes 1 off the log rounded up.

    why it is allowed: By lines 15 and 17, the log of the half is above two less than CC and at most one less, so it rounds up to one less. For 11, about 2.58 rounds up to 3.

  19. the move: use line 18 again and again

    the line:
    after C halvings,rounding up, the logrounded up is 0\begin{gathered} \text{after } C \text{ halvings,} \\ \text{rounding up, the log} \\ \text{rounded up is 0} \end{gathered}

    why it is allowed: While the log rounded up is 1 or more, the log is above 0, so the number is above 1: at least 2. So line 18 takes 1 off each halving, and the number stays 2 or more until exactly CC halvings. For 11, 4, 3, 2, 1, 0.

  20. the move: a log rounded up to 0 belongs to 1

    the line:
    C halvings, rounding up,bring n to 1\begin{gathered} C \text{ halvings, rounding up,} \\ \text{bring } n \text{ to 1} \end{gathered}

    why it is allowed: Such a log is at most 0, so its number is at most 1, and so is 1.

  21. the move: read lines 5 and 7 as doublings

    the line:
    C doublings of 1 reach n,one fewer do not\begin{gathered} C \text{ doublings of 1 reach } n, \\ \text{one fewer do not} \end{gathered}

    why it is allowed: Doubling 1 gives the powers of 2. By line 5, CC doublings get to or past nn; by line 7, one fewer stay below. For 11, 8 stays below and 16 gets past.

  22. the move: count merge sort's rounds

    the line:
    merge sort on this sitemakes C rounds\begin{gathered} \text{merge sort on this site} \\ \text{makes } C \text{ rounds} \end{gathered}
    the row: 5, 9, 2, 7, 11, 1, 8, 3, 10, 6, 411111111111start222221round 1443round 283round 311round 42a run, its length insideno partner, carried downthe first runall in one runa merge in round 2, its numbers59272579goes straight in, the other run used up
    Round 2 merges 5, 9 and 2, 7 into 2, 5, 7, 9.
    after roundruns
    start11
    16
    23
    32
    41
    The first run holds 1, 2, 4, 8, then all 11: four rounds. The count of runs is 11 halved, rounding up.

    why it is allowed: Merge sort starts from runs of 1, whatever the row. Each round merges neighbouring runs in pairs, so the first run doubles, the last round taking only what is left. By line 21 it holds all nn after CC rounds, not fewer.

  23. the move: read it

    the line:
    C halvings,rounding up, andC rounds of merge sort\begin{gathered} C \text{ halvings,} \\ \text{rounding up, and} \\ C \text{ rounds of merge sort} \end{gathered}

    why it is allowed: By lines 20 and 22, one count gives both the halvings and merge sort's rounds. For 11, four of each.

try it with a small nn: at n=11n = 11, line 1 gives C=4C = 4, which you can work out by hand

question 5When do the two roundings of log⁡2 n\log_2\, n differ?
  1. the move: name the gap

    the line:
    D=⌈log⁡2 n⌉−⌊log⁡2 n⌋D = \lceil \log_2\, n \rceil - \lfloor \log_2\, n \rfloor

    why it is allowed: For 11, 4 take away 3 is 1.

  2. the move: never more than 1

    the line:
    D≤1D \le 1
    numberlog× 2+ 110214283164113.46
    11's log lies between 3 and 4.
    the numberits logrounded downrounded upthe gap
    10000
    21110
    3about 1.58121
    42220
    5about 2.32231
    6about 2.58231
    7about 2.81231
    83330
    The gap is 0 at the powers of two, 1, 2, 4 and 8, and 1 elsewhere.

    why it is allowed: Every whole number is at or below the log, so at most its rounding down, or at or above it, so at least its rounding up. So none lies strictly between them, and they differ by 0 or 1.

  3. the move: a power of two gives 0

    the line:
    for a power of two,the gap is 0\begin{gathered} \text{for a power of two,} \\ \text{the gap is 0} \end{gathered}

    why it is allowed: By question 1 its log is whole, and rounding a whole number changes nothing. For 8, 3 both ways.

  4. the move: only a power of two gives 0

    the line:
    a gap of 0 meansa power of two\begin{gathered} \text{a gap of 0 means} \\ \text{a power of two} \end{gathered}

    why it is allowed: Then both roundings are one whole number, the log itself. A log of 3 means the number 8.

  5. the move: read it

    the line:
    0 for a power of two,1 for any other number\begin{gathered} 0 \text{ for a power of two,} \\ 1 \text{ for any other number} \end{gathered}
    the numberthe gapmost looksrounds
    1010
    11144
    16054
    One more look than rounds where the gap is 0.

    why it is allowed: By line 2 the gap is 0 or 1, and by lines 3 and 4 it is 0 just at the powers of two.

  6. the move: compare binary search and merge sort

    the line:
    most looks and rounds agree,except at a power of two\begin{gathered} \text{most looks and rounds agree,} \\ \text{except at a power of two} \end{gathered}

    why it is allowed: The most looks are the log rounded down and 1 more, by question 3; the rounds, the log rounded up, by question 4, none for 1 number. By line 5 they agree except at a power of two.

try it with a small nn: at n=7n = 7, line 1 gives D=1D = 1, which you can work out by hand

a worked example

Halve 20, rounding down and up.

  1. 20 lies between 16 and 32, so its log lies between 4 and 5, about 4.32.
  2. Rounding down: 10, 5, 2, 1, four halvings.
  3. Rounding up: 10, 5, 3, 2, 1, five halvings.
  4. So on 20 numbers, binary search makes at most 4 + 1 = 5 looks, and merge sort 5 rounds.

the trap

check yourself

  1. At most how many looks does binary search make on a row of 3,000 numbers?

    the answer

    3,000 lies between 2,048, eleven 2s, and 4,096, so by question 3, 11 and 1 more, 12.

  2. What is the log to base 2 of 128 rounded down, and of 129 rounded up?

    the answer

    128 is seven 2s, so 7. 129 lies between 128 and 256, so 8, by question 4.

  3. Halve 90 again and again, rounding up. How many halvings reach 1?

    the answer

    90 lies between 64 and 128, so 7, by question 4. By hand, 45, 23, 12, 6, 3, 2, 1.