Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/bit width"

From cppreference.com
< cpp‎ | numeric
m (Text replace - "log2p1" to "bit_width")
m (fmt.)
 
(7 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
{{ddcl|since=c++20|header=bit|
 
{{ddcl|since=c++20|header=bit|
 
template< class T >
 
template< class T >
constexpr T bit_width(T x) noexcept;
+
constexpr int bit_width( T x ) noexcept;
 
}}
 
}}
  
If {{tt|x}} is not zero, calculates the number of bits needed to store the value {{tt|x}}, that is, {{mathjax-or|1=\(1 + \lfloor log_2(x) \rfloor\)|2=1 + floor(log{{su|b=2}}(x))}}. If {{tt|x}} is zero, returns zero.  
+
If {{c|x}} is not zero, calculates the number of bits needed to store the value {{c|x}}, that is, {{mathjax-or|1=\(1 + \lfloor \log_2(x) \rfloor\)|2=1 + floor(log{{su|b=2}}(x))}}. If {{c|x}} is zero, returns zero.
  
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c|unsigned char}}, {{c|unsigned short}}, {{c|unsigned int}}, {{c|unsigned long}}, {{c|unsigned long long}}, or an extended unsigned integer type)}}.
+
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c/core|unsigned char}}, {{c/core|unsigned short}}, {{c/core|unsigned int}}, {{c/core|unsigned long}}, {{c/core|unsigned long long}}, or an extended unsigned integer type)}}.
  
=== Return value ===
+
===Parameters===
Zero if {{tt|x}} is zero; otherwise, one plus the base-2 logarithm of {{tt|x}}, with any fractional part discarded.
+
{{par begin}}
 +
{{par|x|unsigned integer value}}
 +
{{par end}}
  
=== Notes ===
+
===Return value===
 +
Zero if {{c|x}} is zero; otherwise, one plus the base-2 logarithm of {{c|x}}, with any fractional part discarded.
 +
 
 +
===Notes===
 
This function is equivalent to {{c|return std::numeric_limits<T>::digits - std::countl_zero(x);}}.
 
This function is equivalent to {{c|return std::numeric_limits<T>::digits - std::countl_zero(x);}}.
  
=== Example ===
+
{{feature test macro|__cpp_lib_int_pow2|std=C++20|value=202002L|[[cpp/numeric#Bit manipulation (since C++20)|Integral power-of-2 operations]]}}
{{example}}
+
 
 +
===Example===
 +
{{example
 +
|code=
 +
#include <bit>
 +
#include <bitset>
 +
#include <iostream>
 +
 
 +
int main()
 +
{
 +
    for (unsigned x{}; x != 010; ++x)
 +
        std::cout << "bit_width( "
 +
                  << std::bitset<4>{x} << " ) = "
 +
                  << std::bit_width(x) << '\n';
 +
}
 +
|output=
 +
bit_width( 0000 ) = 0
 +
bit_width( 0001 ) = 1
 +
bit_width( 0010 ) = 2
 +
bit_width( 0011 ) = 2
 +
bit_width( 0100 ) = 3
 +
bit_width( 0101 ) = 3
 +
bit_width( 0110 ) = 3
 +
bit_width( 0111 ) = 3
 +
}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=3656|std=C++20|before=the return type of {{tt|bit_width}} is the same as the type of its function argument|after=made it {{c/core|int}}}}
 +
{{dr list end}}
  
=== See also ===
+
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/dsc countl_zero}}
+
{{dsc inc|cpp/numeric/dsc countl_zero}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 13:58, 2 November 2024

 
 
 
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

[edit] Example

#include <bit>
#include <bitset>
#include <iostream>
 
int main()
{
    for (unsigned x{}; x != 010; ++x)
        std::cout << "bit_width( "
                  << std::bitset<4>{x} << " ) = "
                  << std::bit_width(x) << '\n';
}

Output:

bit_width( 0000 ) = 0
bit_width( 0001 ) = 1
bit_width( 0010 ) = 2
bit_width( 0011 ) = 2
bit_width( 0100 ) = 3
bit_width( 0101 ) = 3
bit_width( 0110 ) = 3
bit_width( 0111 ) = 3

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3656 C++20 the return type of bit_width is the same as the type of its function argument made it int

[edit] See also

counts the number of consecutive 0 bits, starting from the most significant bit
(function template) [edit]