std::same_as<T, wchar_t> constexpr bool has_single_bit(T x) noexcept { return x != 0 && (x & (x - 1)) == 0; } 版本二 template <std::unsigned_integral T> requires !std::same_as<T, bool> && !std::same_as<T, char> && !std::same_as<T, char8_t> && !std::...
std::has_single_bit 编辑 定义于头文件 <bit> template< class T >constexpr bool has_single_bit(T x) noexcept; (C++20 起) 检查x 是否为二的整数次幂。 此重载仅若 T 为无符号整数类型(即 unsigned char 、 unsigned short 、 unsigned int 、 unsigned long 、 unsigned long long 或扩展无...
<bitset>#include <cmath>#include <iostream>intmain(){for(autou=0u;u!=10;++u){std::cout<<"u = "<<u<<" = "<<std::bitset<4>(u);if(std::has_single_bit(u))// `ispow2` before P1956R1std::cout<<" = 2^"<<std::log2(u)<<" (is power of two)";std::cout<<'\n'...