From d2f35276bfec59636390cbebe773ede9280a0fb9 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 29 Aug 2025 16:39:19 +0300 Subject: [PATCH] Add util::is_pow2 --- libs/util/include/psemek/util/bits.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/util/include/psemek/util/bits.hpp b/libs/util/include/psemek/util/bits.hpp index 38d7343b..0e5e5d1e 100644 --- a/libs/util/include/psemek/util/bits.hpp +++ b/libs/util/include/psemek/util/bits.hpp @@ -5,8 +5,15 @@ namespace psemek::util { - // log2 rounded down + template + bool is_pow2(T x) + { + static_assert(std::is_integral_v); + return (x > 0) && ((x & (x - 1)) == 0); + } + + // log2 rounded down template T log2(T x) {