Is there a one line expression (possibly boolean) to get the nearest 2^n number for a given integer?
2^n
Example: 5,6,7 must be 8.
Round up to the next higher power of two: see bit-twiddling hacks.
In C:
unsigned int v; // compute the next highest power of 2 of 32-bit v v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++;
2.1m questions
2.1m answers
60 comments
57.0k users