tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
countr_zero.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_BIT_COUNTR_ZERO_HPP
5#define TETL_BIT_COUNTR_ZERO_HPP
6
7#include <etl/_bit/test_bit.hpp>
8#include <etl/_concepts/builtin_unsigned_integer.hpp>
9#include <etl/_limits/numeric_limits.hpp>
10
11namespace etl {
12
13/// \brief Returns the number of consecutive 0 bits in the value of x, starting
14/// from the least significant bit ("right").
15///
16/// \details This overload only participates in overload resolution if UInt is an
17/// unsigned integer type (that is, unsigned char, unsigned short, unsigned int,
18/// unsigned long, unsigned long long, or an extended unsigned integer type).
19///
20/// \returns The number of consecutive 0 bits in the value of x, starting from
21/// the least significant bit.
22///
23/// \ingroup bit
24template <etl::builtin_unsigned_integer UInt>
25[[nodiscard]] constexpr auto countr_zero(UInt x) noexcept -> int
26{
27 auto totalBits = etl::numeric_limits<UInt>::digits;
28 auto result = 0;
29 while (result != totalBits) {
30 if (etl::test_bit(x, static_cast<UInt>(result))) {
31 break;
32 }
33 ++result;
34 }
35 return result;
36}
37
38} // namespace etl
39
40#endif // TETL_BIT_COUNTR_ZERO_HPP
constexpr auto countr_zero(UInt x) noexcept -> int
Returns the number of consecutive 0 bits in the value of x, starting from the least significant bit (...
Definition countr_zero.hpp:25
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18