David Blume's GitList
Repositories
testcode.git
Code
Commits
Branches
Tags
Search
Tree:
bbe8e9c
Branches
Tags
c++11
main
start
testcode.git
product
next_power_of_two.cpp
Add two implementations of round_up_to_next_power_of_two
David Blume
commited
bbe8e9c
at 2019-01-24 20:54:16
next_power_of_two.cpp
Blame
History
Raw
#include <climits> #include "next_power_of_two.hpp" using namespace std; #ifndef USE_TEMPLATE unsigned round_up_to_next_power_of_two(unsigned x) { // https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2 --x; for(size_t i = 1; i < sizeof(x) * CHAR_BIT; i *= 2) x |= x >> i; return ++x; } #endif unsigned another_round_up_to_next_power_of_two(unsigned x) { // https://stackoverflow.com/questions/671815/what-is-the-fastest-most-efficient-way-to-find-the-highest-set-bit-msb-in-an-i return (x > 1) ? 1 << (sizeof(x) * CHAR_BIT - __builtin_clz(x-1)) : x; }