C macro to find the next power of 2 ?
Bakul Shah
bakul at bitblocks.com
Wed Feb 7 10:25:30 UTC 2007
> #define b2(x) ( (x) | ( (x) >> 1) )
> #define b4(x) ( b2(x) | ( b2(x) >> 2) )
> #define b8(x) ( b4(x) | ( b4(x) >> 4) )
> #define b16(x) ( b8(x) | ( b8(x) >> 8) )
> #define b32(x) (b16(x) | (b16(x) >>16) )
> #define next_power_of_2(x) (b32(x-1) + 1)
...
> But i was wondering if there is some simpler code which
> can still be computed as a compile-time constant ?
If this works, why bother? let the compiler work harder!
More information about the freebsd-current
mailing list