[libcxx-commits] [libcxx] [libc++] Optimize std::has_single_bit (PR #133063)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 5 04:07:43 PST 2025


================
@@ -25,7 +25,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <__libcpp_unsigned_integer _Tp>
 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
-  return __t != 0 && (((__t & (__t - 1)) == 0));
+  return (__t ^ (__t - 1)) > __t - 1;
----------------
philnik777 wrote:

We don't support MSVC, so using the builtin is fine.

https://github.com/llvm/llvm-project/pull/133063


More information about the libcxx-commits mailing list