tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bit_width.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_BIT_WIDTH_HPP
5#define TETL_BIT_BIT_WIDTH_HPP
6
7#include <etl/_bit/countl_zero.hpp>
8#include <etl/_concepts/builtin_unsigned_integer.hpp>
9#include <etl/_limits/numeric_limits.hpp>
10
11namespace etl {
12
13/// \brief If x is not zero, calculates the number of bits needed to store the
14/// value x, that is, 1+⌊log2(x)⌋. If x is zero, returns zero.
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/// \ingroup bit
21template <etl::builtin_unsigned_integer UInt>
22[[nodiscard]] constexpr auto bit_width(UInt x) noexcept -> int
23{
24 return etl::numeric_limits<UInt>::digits - etl::countl_zero(x);
25}
26
27} // namespace etl
28
29#endif // TETL_BIT_BIT_WIDTH_HPP
constexpr auto bit_width(UInt x) noexcept -> int
If x is not zero, calculates the number of bits needed to store the value x, that is,...
Definition bit_width.hpp:22
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18