Defined in header <bit> | ||
---|---|---|
template< class T > constexpr bool has_single_bit( T x ) noexcept; | (since C++20) |
Checks if x
is an integral power of two.
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 | - | value of unsigned integer type |
true
if x
is an integral power of two; otherwise false
.
Feature-test macro | Value | Std | Comment |
---|---|---|---|
__cpp_lib_int_pow2 | 202002L | (C++20) | Integral power-of-2 operations |
First version |
---|
Second version |
#include <bit> #include <bitset> #include <iostream> #include <cmath> int main() { for (auto u = 0u; u != 10; ++u) { std::cout << "u = " << u << " = " << std::bitset<4>(u); if (std::has_single_bit(u)) // `ispow2` before P1956R1 std::cout << " = 2^" << std::log2(u) << " (is power of two)"; std::cout << '\n'; } }
Output:
(C++20) | counts the number of 1 bits in an unsigned integer (function template) |
returns the number of bits set to true (public member function of std::bitset<N> ) |
|
accesses specific bit (public member function of std::bitset<N> ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/numeric/has_single_bit