RESTinio

bitops.hpp
Go to the documentation of this file.
1/*
2 * restinio
3 */
4
9#pragma once
10
11#include <cstdint>
12
13 namespace restinio {
14
15 namespace utils{
16
17 namespace impl {
18
19 namespace bitops {
20
21 namespace details {
22
23 template< typename T >
24 constexpr T mask( unsigned bits_to_extract )
25{
26 return bits_to_extract <= 1u ? T{1} :
27 static_cast<T>((mask<T>(bits_to_extract-1) << 1) | T{1});
28}
29
30 template< typename T >
31 struct bits_count;
32
33 template<>
34 struct bits_count<std::uint8_t> { static constexpr unsigned count = 8u; };
35
36 template<>
37 struct bits_count<std::int8_t> { static constexpr unsigned count = 8u; };
38
39 template<>
40 struct bits_count<char> { static constexpr unsigned count = 8u; };
41
42 template<>
43 struct bits_count<std::uint16_t> { static constexpr unsigned count = 16u; };
44
45 template<>
46 struct bits_count<std::int16_t> { static constexpr unsigned count = 16u; };
47
48 template<>
49 struct bits_count<std::uint32_t> { static constexpr unsigned count = 32u; };
50
51 template<>
52 struct bits_count<std::int32_t> { static constexpr unsigned count = 32u; };
53
54 template<>
55 struct bits_count<std::uint64_t> { static constexpr unsigned count = 64u; };
56
57 template<>
58 struct bits_count<std::int64_t> { static constexpr unsigned count = 64u; };
59
60} /* namespace details */
61
80 template<
81 typename T,
82 unsigned Shift,
83 unsigned Bits_To_Extract = details::bits_count<T>::count,
84 typename F = unsigned int >
85T
86 n_bits_from( F value )
87{
88 return static_cast<T>(value >> Shift) & details::mask<T>(Bits_To_Extract);
89}
90
91} /* namespace bitops */
92
93} /* namespace impl */
94
95} /* namespace utils */
96
97} /* 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