std::ispow2
提供: cppreference.com
| ヘッダ <bit> で定義
|
||
| template< class T > constexpr bool ispow2(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> constexpr bool ispow2(T x) noexcept { return x != 0 && (x & (x - 1)) == 0; } |
[編集] 例
Run this code
#include <bit> #include <iostream> int main() { std::cout << std::boolalpha; for (auto i = 0u; i < 10u; ++i) { std::cout << "ispow2(" << i << ") = " << std::ispow2(i) << '\n'; } }
出力:
ispow2(0) = false ispow2(1) = true ispow2(2) = true ispow2(3) = false ispow2(4) = true ispow2(5) = false ispow2(6) = false ispow2(7) = false ispow2(8) = true ispow2(9) = false