// This file is part of the mcl project. // Copyright (c) 2022 merryhime // SPDX-License-Identifier: MIT #pragma once #include #include "mcl/bitsizeof.hpp" #include "mcl/concepts/bit_integral.hpp" #include "mcl/stdint.hpp" namespace mcl::bit { template inline size_t count_ones(T x) { return std::bitset>(x).count(); } template constexpr size_t count_leading_zeros(T x) { size_t result = bitsizeof; while (x != 0) { x >>= 1; result--; } return result; } template constexpr int highest_set_bit(T x) { int result = -1; while (x != 0) { x >>= 1; result++; } return result; } template constexpr size_t lowest_set_bit(T x) { if (x == 0) { return bitsizeof; } size_t result = 0; while ((x & 1) == 0) { x >>= 1; result++; } return result; } } // namespace mcl::bit