ILOG2(3) Library Functions Manual ILOG2(3)

ilog2
integer base-2 logarithm

#include <sys/bitops.h>

int
ilog2(size x);

For positive x, the ilog2() macro returns the integer part of the base-2 logarithm of x; that is, floor(log_2(x));. If n = ilog2(x), then n is the largest integer such that 2**n <= x; in other words, ilog2() returns the largest integer to which 2 can be raised to obtain a value at most x.

The type of the input parameter must be unsigned.

If x is a constant expression, then so is ilog2(x).

ilog2() returns -1 when x is zero.

bitops(3), ffs(3), ilogb(3), log2(3), imax(9), powerof2(9)

The ilog2() macro first appeared in NetBSD 5.0.
November 5, 2021 NetBSD 10.1