lowest_bit.hpp 865 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // -----------------------------------------------------------
  2. // lowest_bit()
  3. //
  4. // Position of the lowest bit that is set.
  5. //
  6. // Copyright (c) 2003-2004, 2008, 2025 Gennaro Prota
  7. //
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // -----------------------------------------------------------
  13. #ifndef BOOST_LOWEST_BIT_HPP_GP_20030301
  14. #define BOOST_LOWEST_BIT_HPP_GP_20030301
  15. #include "boost/assert.hpp"
  16. #include "boost/core/bit.hpp"
  17. #include <type_traits>
  18. namespace boost {
  19. namespace detail {
  20. template< typename T >
  21. int
  22. lowest_bit( T x )
  23. {
  24. BOOST_ASSERT( x >= 1 );
  25. return boost::core::countr_zero( static_cast< typename std::make_unsigned< T >::type >( x ) );
  26. }
  27. }
  28. }
  29. #endif // include guard