tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
countl_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_COUNTL_ZERO_HPP
5#define TETL_BIT_COUNTL_ZERO_HPP
6
7#include <etl/_concepts/builtin_unsigned_integer.hpp>
8#include <etl/_limits/numeric_limits.hpp>
9
10namespace etl {
11
12/// \brief Returns the number of consecutive 0 bits in the value of x, starting
13/// from the most significant bit ("left").
14///
15/// \details This overload only participates in overload resolution if UInt is an
16/// unsigned integer type (that is, unsigned char, unsigned short, unsigned int,
17/// unsigned long, unsigned long long, or an extended unsigned integer type).
18///
19/// \returns The number of consecutive 0 bits in the value of x, starting from
20/// the most significant bit.
21///
22/// \ingroup bit
23template <etl::builtin_unsigned_integer UInt>
24[[nodiscard]] constexpr auto countl_zero(UInt x) noexcept -> int
25{
26 auto const totalBits = etl::numeric_limits<UInt>::digits;
27 if (x == UInt(0)) {
28 return etl::numeric_limits<UInt>::digits;
29 }
30
31 auto res = 0;
32 while (!(x & (UInt(1) << (static_cast<UInt>(totalBits) - UInt(1))))) {
33 x = static_cast<UInt>(x << UInt(1));
34 ++res;
35 }
36
37 return res;
38}
39
40} // namespace etl
41
42#endif // TETL_BIT_COUNTL_ZERO_HPP
constexpr auto countl_zero(UInt x) noexcept -> int
Returns the number of consecutive 0 bits in the value of x, starting from the most significant bit ("...
Definition countl_zero.hpp:24
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18