Phil CK

Power Of Two Utils

Last updated on

Power of two numbers are a staple of programming they have very nice properties. These two helpers come in handy time and time again.

#include <math.h>
#include <stdint.h>


int
is_power_of_two(const uint32_t number) {
        return (number>0 && ((number & (number-1)) == 0));
}


uint32_t
next_power_of_two(const uint32_t number) {
        return pow(2, ceil(log(number)/log(2)));
}