[Solved] Finding closest power of 2 for any float at compile time


This does it:

#define CALC_SCALE ( x > 16 ? 32 : x > 8 ? 16 : x > 4 ? 8 : x > 2 ? 4 : x > 1 ? 2 : 1 )

At compile time:

int x = CALC_SCALE( 13 );

is compiled to:

int x = 16;

This can easily be changed to support floats.

solved Finding closest power of 2 for any float at compile time