xieite::pow_of_2()

Defined in fragment xieite:pow_of_2
(header-only: <xieite/math/pow_of_2.hpp>)


Checks if an integer is a power of 2. The algorithm is stolen from [Bit Twiddling Hacks].


Declaration

template<std::unsigned_integral T>
[[nodiscard]] constexpr bool pow_of_2(T value) noexcept;


Example

import xieite;

int main() {
	xieite::dump(xieite::pow_of_2(4u));
	xieite::dump(xieite::pow_of_2(0u));
}
Output:
true
false
[View in Compiler Explorer]