David Blume's GitList
Repositories
testcode.git
Code
Commits
Branches
Tags
Search
Tree:
56e9725
Branches
Tags
c++11
main
start
testcode.git
product
next_power_of_two.cpp
Add .clang-format
dblume
commited
56e9725
at 2023-08-12 23:24:26
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; }