◄ WORLD V · SONNY 5DART 419 · a helldive at the net

THE INTEGER LOG2 the position of the highest set bit — a logarithm by counting shifts

The floor of log₂(n) is just the index of the highest set bit — how many times you can halve n before it reaches 1. It tells you how many bits a number needs, which bucket it falls in, what power of two brackets it. The naive form counts shifts; the fast forms use a De Bruijn hash or a hardware count-leading-zeros instruction. All return the identical integer. It is the discrete logarithm every allocator, every float unpacker, and every binary-search bound quietly uses.

THE TECHNIQUE floor(log₂ n) = highest set-bit index = shifts until 1

The demo computes ⌊log₂(37)⌋ = 5 by counting how many right-shifts bring 37 down to 1: live demo


HISTORY & CREDIT integer log2 · highest-set-bit / CLZ

“Logarithms are floating-point.” — the integer log2 is a bit position: no math library, just the top set bit. cited

the meaning · ⌊log₂ n⌋ = index of the most significant 1 — the number of bits minus one.
the forms · count shifts (simple); De Bruijn hash of the top bit; hardware CLZ/BSR (one instruction).
the uses · allocator size classes, float exponent extraction, binary-search depth — all lean on it.

A logarithm read straight off the bits — the highest 1’s position, by count, hash, or instruction. resource

RECOMMEND FOR I-13 highest-set-bit, on the compiler

On the canonical compiler, counting shifts of 37 down to 1 gives ⌊log₂(37)⌋ = 5 (32 ≤ 37 < 64):

$ i13 run tk_log2.i13 # count right-shifts until n <= 1 RUN OK · 82 step(s) · peak stack 4 · call depth 6 v = 37 l = 5 -- 2^5 = 32 <= 37 < 64 same = 1 -- floor(log2 37)
Recommend as a NULL — resource, one function many mechanisms. Integer log2 has a shift-count form, a De Bruijn form (see dart 414), and a single-instruction CLZ form — and all return the identical 5. That is the keeper criterion’s tell: many correct mechanisms, one value, so what varies (steps, table, silicon) is resource (B40). NULL. It is the plainest “the answer is a bit position” case, and it wires to the De Bruijn dart as the trick that speeds it up.