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.hpp
Add .clang-format
dblume
commited
56e9725
at 2023-08-12 23:24:26
next_power_of_two.hpp
Blame
History
Raw
#pragma once #define USE_TEMPLATE #ifdef USE_TEMPLATE #include <cstddef> #include <type_traits> #include <climits> template <typename T> T round_up_to_next_power_of_two(T x) { // https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2 static_assert(std::is_unsigned<T>::value, "x must be an unsigned type"); --x; for (size_t i = 1; i < sizeof(x) * CHAR_BIT; i *= 2) x |= x >> i; return ++x; } #else unsigned round_up_to_next_power_of_two(unsigned x); #endif unsigned another_round_up_to_next_power_of_two(unsigned x);