From d62d0ac48997f9b72da0f554dcf5269694d5999e Mon Sep 17 00:00:00 2001 From: smatz Date: Fri, 5 Feb 2010 17:05:58 +0000 Subject: (svn r19019) -Codechange: use HasExactlyOneBit() and HasAtMostOneBit() instead of CountBits() where possible --- src/core/bitmath_func.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/core') diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index 6ef67ca7f..85eaee6f9 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -254,6 +254,30 @@ static inline uint CountBits(T value) return num; } +/** + * Test whether \a value has exactly 1 bit set + * + * @param value the value to test. + * @return does \a value have exactly 1 bit set? + */ +template +static FORCEINLINE bool HasExactlyOneBit(T value) +{ + return value != 0 && (value & (value - 1)) == 0; +} + +/** + * Test whether \a value has at most 1 bit set + * + * @param value the value to test. + * @return does \a value have at most 1 bit set? + */ +template +static FORCEINLINE bool HasAtMostOneBit(T value) +{ + return (value & (value - 1)) == 0; +} + /** * ROtate x Left by n * -- cgit v1.2.3-54-g00ecf