From a345b894d3e6cc4b0b4fdd02c200217df6b3a425 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Mon, 15 Jun 2020 14:10:07 +0800 Subject: [PATCH] [math_utils] Add is_power_of_two() --- base/math_utils.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/base/math_utils.h b/base/math_utils.h index 9c1bb0c..de00d88 100644 --- a/base/math_utils.h +++ b/base/math_utils.h @@ -34,6 +34,11 @@ namespace base { T div_down(T const &v, T const &divisor) { return v / divisor; } + + template + bool is_power_of_two(T const v) { + return !(v & (v - 1)); + } } //----------------------------------------------------------------