名前空間
変種

std::has_single_bit

提供: cppreference.com
<tbody> </tbody>
ヘッダ <bit> で定義
template< class T > constexpr bool has_single_bit(T x) noexcept;
(C++20以上)

x が2の整数乗かどうか調べます。

このオーバーロードは、T が符号なし整数型 (つまり unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long または拡張符号なし整数型) である場合にのみ、オーバーロード解決に参加します。

戻り値

x が2の整数乗であれば true、そうでなければ false

実装例

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 != 0 && (x & (x - 1)) == 0;
}

#include