General Utility Library for C++14  2.8
bit_manip.h
Go to the documentation of this file.
1 
24 #ifndef GUL14_BIT_MANIP_H_
25 #define GUL14_BIT_MANIP_H_
26 
27 #include <cassert>
28 #include <type_traits>
29 
30 #include "gul14/internal.h"
31 
32 namespace gul14 {
33 
46 template <typename T>
48  std::enable_if_t<
49  std::is_integral<T>::value
50  and not std::is_same<std::decay_t<T>, bool>::value,
51  std::decay_t<T>
52  >;
53 
76 template <typename T = unsigned, typename ReturnT = BitFunctionReturnType<T>>
77 auto constexpr inline bit_set(unsigned bit) noexcept -> ReturnT {
78  assert(bit < sizeof(T) * 8);
79  return static_cast<ReturnT>(std::make_unsigned_t<T>{1u} << bit);
80 }
81 
102 template <typename T, typename ReturnT = BitFunctionReturnType<T>>
103 auto constexpr inline bit_set(T previous, unsigned bit) noexcept -> ReturnT {
104  return previous | bit_set<T>(bit);
105 }
106 
127 template <typename T, typename ReturnT = BitFunctionReturnType<T>>
128 auto constexpr inline bit_reset(T previous, unsigned bit) noexcept -> ReturnT {
129  return static_cast<ReturnT>(previous & ~bit_set<T>(bit));
130 }
131 
154 template <typename T, typename ReturnT = BitFunctionReturnType<T>>
155 auto constexpr inline bit_flip(T previous, unsigned bit) noexcept -> ReturnT {
156  return previous ^ bit_set<T>(bit);
157 }
158 
179 template <typename T>
180 bool constexpr inline bit_test(T bits, unsigned bit) noexcept {
181  return bits & bit_set<T>(bit);
182 }
183 
184 } // namespace gul14
185 
186 #endif
187 
188 // vi:ts=4:sw=4:et
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
constexpr auto bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition: bit_manip.h:77
constexpr auto bit_flip(T previous, unsigned bit) noexcept -> ReturnT
Flip a bit in an integral value.
Definition: bit_manip.h:155
constexpr bool bit_test(T bits, unsigned bit) noexcept
Test a bit in an integral value.
Definition: bit_manip.h:180
constexpr auto bit_reset(T previous, unsigned bit) noexcept -> ReturnT
Reset a bit in an integral value.
Definition: bit_manip.h:128
std::enable_if_t< std::is_integral< T >::value and not std::is_same< std::decay_t< T >, bool >::value, std::decay_t< T > > BitFunctionReturnType
Return type of the bit manipulation functions.
Definition: bit_manip.h:52