tetl
0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
countl_one.hpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: BSL-1.0
2
3
#ifndef TETL_BIT_COUNTL_ONE_HPP
4
#define TETL_BIT_COUNTL_ONE_HPP
5
6
#include <
etl/_concepts/builtin_unsigned_integer.hpp
>
7
#include <
etl/_limits/numeric_limits.hpp
>
8
9
namespace
etl
{
10
22
template
<etl::builtin_
unsigned
_
int
eger UInt>
23
[[nodiscard]]
constexpr
auto
countl_one
(UInt x)
noexcept
->
int
24
{
25
auto
const
totalBits =
etl::numeric_limits<UInt>::digits
;
26
if
(x ==
etl::numeric_limits<UInt>::max
()) {
27
return
totalBits;
28
}
29
30
auto
res = 0;
31
while
(x & (UInt(1) << (
static_cast<
UInt
>
(totalBits) - UInt(1)))) {
32
x =
static_cast<
UInt
>
(x << UInt(1));
33
++res;
34
}
35
36
return
res;
37
}
38
39
}
// namespace etl
40
41
#endif
// TETL_BIT_COUNTL_ONE_HPP
builtin_unsigned_integer.hpp
etl::countl_one
constexpr auto countl_one(UInt x) noexcept -> int
Returns the number of consecutive 1 ("one") bits in the value of x, starting from the most significan...
Definition
countl_one.hpp:23
etl
Definition
adjacent_find.hpp:8
numeric_limits.hpp
etl::numeric_limits::digits
static constexpr int digits
Definition
numeric_limits.hpp:24
etl::numeric_limits::max
static constexpr auto max() noexcept
Definition
numeric_limits.hpp:21
include
etl
_bit
countl_one.hpp
Generated on Sat Mar 8 2025 16:19:59 for tetl by
1.14.0