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. – ToolmakerSteve Sep 6 '14 at 5:23python
: stackoverflow.com/a/19164783/1959808 – Ioannis Filippidis Nov 16 '14 at 11:47