Namensräume
Varianten
Aktionen

std::has_single_bit

Von cppreference.com
< cpp‎ | numeric
 
 
Dienstprogramm-Bibliotheken
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Coroutine-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

Allgemeine Hilfsmittel
Relationale Operatoren (in C++20 veraltet)
 
Bitmanipulation
(C++20)
(C++23)
Ganzzahlige Zweierpotenzen
has_single_bit
(C++20)
(C++20)
(C++20)
(C++20)
Rotation
(C++20)
(C++20)
Zählen
(C++20)
(C++20)
(C++20)
Endianheit
(C++20)
 
Definiert in Header <bit>
template< class T >
constexpr bool has_single_bit( T x ) noexcept;
(seit C++20)

Prüft, ob x eine ganzzahlige Zweierpotenz ist.

Diese Überladung nimmt an der Überladungsauflösung teil, nur wenn T ein vorzeichenloser Ganzzahltyp ist (d.h. unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long oder ein erweiterter vorzeichenloser Ganzzahltyp).

Inhalt

[edit] Parameter

x - Wert vom Typ vorzeichenlose Ganzzahl

[edit] Rückgabewert

true, wenn x eine ganzzahlige Zweierpotenz ist; andernfalls false.

[edit] Hinweise

Vor P1956R1 war der vorgeschlagene Name für diese Funktion-Vorlage ispow2.

Feature-Test-Makro Wert Std Feature
__cpp_lib_int_pow2 202002L (C++20) Operationen mit Zweierpotenzen

[edit] Mögliche Implementierung

Erste 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));
}
Zweite 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] Beispiel

#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';
    }
}

Ausgabe

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] Siehe auch

(C++20)
zählt die Anzahl der 1-Bits in einer vorzeichenlosen Ganzzahl
(Funktion-Template) [bearbeiten]
gibt die Anzahl der auf true gesetzten Bits zurück
(public member function of std::bitset<N>) [edit]
greift auf ein bestimmtes Bit zu
(public member function of std::bitset<N>) [edit]