I need to find the smallest power of two that's greater or equal to a given value. So far, I have this:
int value = 3221; // 3221 is just an example, could be any number
int result = 1;
while (result < value) result <<= 1;
It works fine, but feels kind of naive. Is there a better algorithm for that problem?
EDIT. There were some nice Assembler suggestions, so I'm adding those tags to the question.
1 << r
to get the power of 2. – ToolmakerStevepython
: stackoverflow.com/a/19164783/1959808 – Ioannis Filippidisbit
header. The same answer cannot be given to a c question. – Jake Schmidt