GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::bit_width(3) C++ Standard Libary std::bit_width(3)

std::bit_width - std::bit_width


Defined in header <bit>
template< class T > (since C++20)
constexpr int bit_width( T x ) noexcept;


If x is not zero, calculates the number of bits needed to store the value x, that
is, \(1 + \lfloor \log_2(x) \rfloor\)1 + floor(log
2(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).


x - unsigned integer value


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


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


Feature-test macro: __cpp_lib_int_pow2

// Run this code


#include <bit>
#include <bitset>
#include <iostream>


int main()
{
for (unsigned x{0}; x != 8; ++x)
{
std::cout
<< "bit_width( "
<< std::bitset<4>{x} << " ) = "
<< std::bit_width(x) << '\n';
}
}


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


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 made it int
type of its function argument


countl_zero counts the number of consecutive 0 bits, starting from the most
(C++20) significant bit
(function template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.