tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
countr_one.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_BIT_COUNTR_ONE_HPP
4#define TETL_BIT_COUNTR_ONE_HPP
5
9
10namespace etl {
11
23template <etl::builtin_unsigned_integer UInt>
24[[nodiscard]] constexpr auto countr_one(UInt x) noexcept -> int
25{
26 auto totalBits = etl::numeric_limits<UInt>::digits;
27 auto result = 0;
28 while (result != totalBits) {
29 if (not etl::test_bit(x, static_cast<UInt>(result))) {
30 break;
31 }
32 ++result;
33 }
34 return result;
35}
36
37} // namespace etl
38
39#endif // TETL_BIT_COUNTR_ONE_HPP
constexpr auto test_bit(UInt word, UInt pos) noexcept -> bool
Test bit at position pos.
Definition test_bit.hpp:19
constexpr auto countr_one(UInt x) noexcept -> int
Returns the number of consecutive 1 bits in the value of x, starting from the least significant bit (...
Definition countr_one.hpp:24
Definition adjacent_find.hpp:8
static constexpr int digits
Definition numeric_limits.hpp:24