Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/has single bit"

From cppreference.com
< cpp‎ | numeric
m (headers sorted)
m (fmt.)
 
Line 8: Line 8:
 
Checks if {{c|x}} is an integral power of two.
 
Checks if {{c|x}} is an integral power of two.
  
{{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)}}.
  
 
===Parameters===
 
===Parameters===
Line 19: Line 19:
  
 
===Notes===
 
===Notes===
 +
{{petty|Prior to {{stddoc|P1956R1}}, the proposed name for this function template was {{tt|ispow2}}.}}
 +
 
{{feature test macro|__cpp_lib_int_pow2|[[cpp/numeric#Bit manipulation (since C++20)|Integral power-of-2 operations]]|std=C++20|value=202002L}}
 
{{feature test macro|__cpp_lib_int_pow2|[[cpp/numeric#Bit manipulation (since C++20)|Integral power-of-2 operations]]|std=C++20|value=202002L}}
  
Line 53: Line 55:
 
int main()
 
int main()
 
{
 
{
     for (auto u = 0u; u != 10; ++u)
+
     for (auto u{0u}; u != 0B1010; ++u)
 
     {
 
     {
 
         std::cout << "u = " << u << " = " << std::bitset<4>(u);
 
         std::cout << "u = " << u << " = " << std::bitset<4>(u);
         if (std::has_single_bit(u)) // `ispow2` before P1956R1
+
         if (std::has_single_bit(u))
 
             std::cout << " = 2^" << std::log2(u) << " (is power of two)";
 
             std::cout << " = 2^" << std::log2(u) << " (is power of two)";
 
         std::cout << '\n';
 
         std::cout << '\n';

Latest revision as of 13:39, 2 November 2024

 
 
 
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).

Contents

[edit] Parameters

x - value of unsigned integer type

[edit] Return value

true if x is an integral power of two; otherwise false.

[edit] Notes

Prior to P1956R1, the proposed name for this function template was ispow2.

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

[edit] Possible implementation

First version
template<std::unsigned_integral T>
    requires !std::same_as<T, bool> && !std::same_as<T, char> &&
             !std::same_as<T, char8_t> && !std::same_as<T, char16_t> &&
             !std::same_as<T, char32_t> && !std::same_as<T, wchar_t>
constexpr bool has_single_bit(T x) noexcept
{
    return x && !(x & (x - 1));
}
Second version
template<std::unsigned_integral T>
    requires !std::same_as<T, bool> && !std::same_as<T, char> &&
             !std::same_as<T, char8_t> && !std::same_as<T, char16_t> &&
             !std::same_as<T, char32_t> && !std::same_as<T, wchar_t>
constexpr bool has_single_bit(T x) noexcept
{
    return std::popcount(x) == 1;
}

[edit] Example

#include <bit>
#include <bitset>
#include <cmath>
#include <iostream>
 
int main()
{
    for (auto u{0u}; u != 0B1010; ++u)
    {
        std::cout << "u = " << u << " = " << std::bitset<4>(u);
        if (std::has_single_bit(u))
            std::cout << " = 2^" << std::log2(u) << " (is power of two)";
        std::cout << '\n';
    }
}

Output:

u = 0 = 0000
u = 1 = 0001 = 2^0 (is power of two)
u = 2 = 0010 = 2^1 (is power of two)
u = 3 = 0011
u = 4 = 0100 = 2^2 (is power of two)
u = 5 = 0101
u = 6 = 0110
u = 7 = 0111
u = 8 = 1000 = 2^3 (is power of two)
u = 9 = 1001

[edit] See also

(C++20)
counts the number of 1 bits in an unsigned integer
(function template) [edit]
returns the number of bits set to true
(public member function of std::bitset<N>) [edit]
accesses specific bit
(public member function of std::bitset<N>) [edit]