STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Calculating log2(x) for Integers and at Compile-Time

STXXL provides three methods to calculate log2(x), which is often needed for binary trees, etc.

The first is during compile-time using template meta-programming magic:

std::cout << stxxl::LOG2<10000>::floor << std::endl;
std::cout << stxxl::LOG2<10000>::ceil << std::endl;

The second is for calculating log2(x) for integer arguments using simple bit shift arithmetic:

std::cout << stxxl::ilog2_floor(10000) << std::endl;
std::cout << stxxl::ilog2_ceil(10000) << std::endl;

The third and probably least useful is to use conversion to double and math.h's facilities:

std::cout << stxxl::log2_floor(10000) << std::endl;
std::cout << stxxl::log2_ceil(10000) << std::endl;