bitops.hpp
Go to the documentation of this file.
/*
* restinio
*/
#pragma once
#include <cstdint>
namespace restinio {
namespace utils{
namespace impl {
namespace bitops {
namespace details {
template< typename T >
constexpr T mask( unsigned bits_to_extract )
{
return bits_to_extract <= 1u ? T{1} :
static_cast<T>((mask<T>(bits_to_extract-1) << 1) | T{1});
}
template< typename T >
struct bits_count;
template<>
struct bits_count<std::uint8_t> { static constexpr unsigned count = 8u; };
template<>
struct bits_count<std::int8_t> { static constexpr unsigned count = 8u; };
template<>
struct bits_count<char> { static constexpr unsigned count = 8u; };
template<>
struct bits_count<std::uint16_t> { static constexpr unsigned count = 16u; };
template<>
struct bits_count<std::int16_t> { static constexpr unsigned count = 16u; };
template<>
struct bits_count<std::uint32_t> { static constexpr unsigned count = 32u; };
template<>
struct bits_count<std::int32_t> { static constexpr unsigned count = 32u; };
template<>
struct bits_count<std::uint64_t> { static constexpr unsigned count = 64u; };
template<>
struct bits_count<std::int64_t> { static constexpr unsigned count = 64u; };
} /* namespace details */
template<
typename T,
unsigned Shift,
unsigned Bits_To_Extract = details::bits_count<T>::count,
typename F = unsigned int >
T
n_bits_from( F value )
{
return static_cast<T>(value >> Shift) & details::mask<T>(Bits_To_Extract);
}
} /* namespace bitops */
} /* namespace impl */
} /* namespace utils */
} /* namespace restinio */
restinio::utils::impl::bitops::details::mask
constexpr T mask(unsigned bits_to_extract)
Definition: bitops.hpp:24
restinio::utils::impl::bitops::n_bits_from
T n_bits_from(F value)
Extract N bits from a bigger integer value.
Definition: bitops.hpp:86
restinio
Definition: asio_include.hpp:21
std
STL namespace.
restinio::utils::impl::bitops::details::bits_count
Definition: bitops.hpp:31