From 61027e530379fcdb202715ca3f9933252730c84e Mon Sep 17 00:00:00 2001 From: Pankaj Raghav Date: Sat, 29 Jan 2022 12:45:21 +0530 Subject: [PATCH] AK: Add is_power_of_2 helper --- AK/StdLibExtras.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/AK/StdLibExtras.h b/AK/StdLibExtras.h index 2fcb8565b9..1a255ede40 100644 --- a/AK/StdLibExtras.h +++ b/AK/StdLibExtras.h @@ -16,6 +16,12 @@ constexpr auto round_up_to_power_of_two(T value, U power_of_two) requires(IsInte return ((value - 1) & ~(power_of_two - 1)) + power_of_two; } +template +constexpr bool is_power_of_two(T value) requires(IsIntegral) +{ + return value && !((value) & (value - 1)); +} + // HACK: clang-format does not format this correctly because of the requires clause above. // Disabling formatting for that doesn't help either. //