/* roundup2.c : round up a value to next power of 2 * PUBLIC DOMAIN - Jon Mayo - December 10, 2008 */ #include #include /* rounds up n to next power of 2 * does not do well with n bigger than 0x80000000-1 */ unsigned roundup2(unsigned n) { unsigned s; n=abs(n); for(s=1;s<32;s<<=1) { n|=n>>s; } return n+1; } int main(int argc, char **argv) { int i; printf("%8s %8s %8s\n", "original", "over", "roundup2"); for(i=1;i