Namespaces
Variants
Actions

std::bit_width

From cppreference.com
< cpp‎ | numeric
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Bit manipulation
(C++20)
(C++23)
Integral powers of 2
(C++20)
(C++20)
bit_width
(C++20)
Rotating
(C++20)
(C++20)
Counting
(C++20)
(C++20)
(C++20)
Endian
(C++20)
 
Defined in header <bit>
template< class T >
constexpr int bit_width( T x ) noexcept;
(since C++20)

If x is not zero, calculates the number of bits needed to store the value x, that is, 1 + floor(log2(x)). If x is zero, returns zero.

This overload participates in overload resolution only if T is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Contents

[edit] Parameters

x - unsigned integer value

[edit] Return value

Zero if x is zero; otherwise, one plus the base-2 logarithm of x, with any fractional part discarded.

[edit] Notes

This function is equivalent to return std::numeric_limits<T>::digits - std::countl_zero(x);.

Feature-test macro Value Std Feature
__cpp_lib_int_pow2 202002L (C++20) Integral power-of-2 operations

[