1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/bits.hpp
2018-12-15 16:21:52 +00:00

37 lines
805 B
C++

#ifndef LLARP_BITS_HPP
#define LLARP_BITS_HPP
#include <cstddef>
namespace llarp
{
namespace bits
{
template < typename Int_t >
constexpr std::size_t
count_bits(const Int_t& i)
{
return i == 0 ? 0
: ((i & 0x01) == 0x01) ? 1UL + count_bits(i >> 1)
: count_bits(i >> 1);
}
template < typename T >
constexpr std::size_t
__count_array_bits(const T& array, std::size_t idx)
{
return idx < sizeof(T)
? count_bits(array[idx]) + __count_array_bits(array, idx + 1)
: 0;
}
template < typename T >
constexpr std::size_t
count_array_bits(const T& array)
{
return __count_array_bits(array, 0);
}
} // namespace bits
} // namespace llarp
#endif