I just wonder if there exists better (i.e. faster?) way to get the nextpower of 2 for a given number than the following one (maybe somebetter sort of bit-twiddling hack is possible?) ...static size_tnpow2(size_t n){        n--;        n |= n >> 1;        n |= n >> 2;        n |= n >> 4;        n |= n >> 8;        n |= n >> 16;        return n + 1;}Thanks for your ideas.
Next power of 2 for a number (in search for better bit-twiddling way)
c;optimization;low level;bitwise operators;arithmetic
According to Sean Eron Anderson's collection of Bit Twiddling Hacks, that's probably the fastest way.The alternatives are an IEEE float casting hack, which involves fewer C operations but ends up slower, and directly using the x86 BSR (bit scan reverse) machine instruction, but in Anderson's testing that didn't end up significantly faster than the above OR-ing trick.