General Utility Library for C++14  2.11
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 
52 template <typename T>
54  std::enable_if_t<
55  std::is_integral<T>::value
56  and not std::is_same<std::decay_t<T>, bool>::value,
57  std::decay_t<T>
58  >;
59 
79 enum class endian
80 {
81 #if defined(__BYTE_ORDER__)
82  little = __ORDER_LITTLE_ENDIAN__,
83  big = __ORDER_BIG_ENDIAN__,
84  native = __BYTE_ORDER__
85 #elif defined(_MSC_VER) && !defined(__clang__)
86  little = 0,
87  big = 1,
88  native = little
89 #else
90  #error "Don't know how to determine machine endianness on this compiler"
91  // Just for Doxygen:
92  little,
93  big,
94  native
95 #endif
96 };
97 
120 template <typename T = unsigned, typename ReturnT = BitFunctionReturnType<T>>
121 auto constexpr inline bit_set(unsigned bit) noexcept -> ReturnT {
122  assert(bit < sizeof(T) * 8);
123  return static_cast<ReturnT>(std::make_unsigned_t<T>{1u} << bit);
124 }
125 
146 template <typename T, typename ReturnT = BitFunctionReturnType<T>>
147 auto constexpr inline bit_set(T previous, unsigned bit) noexcept -> ReturnT {
148  return previous | bit_set<T>(bit);
149 }
150 
171 template <typename T, typename ReturnT = BitFunctionReturnType<T>>
172 auto constexpr inline bit_reset(T previous, unsigned bit) noexcept -> ReturnT {
173  return static_cast<ReturnT>(previous & ~bit_set<T>(bit));
174 }
175 
198 template <typename T, typename ReturnT = BitFunctionReturnType<T>>
199 auto constexpr inline bit_flip(T previous, unsigned bit) noexcept -> ReturnT {
200  return previous ^ bit_set<T>(bit);
201 }
202 
223 template <typename T>
224 bool constexpr inline bit_test(T bits, unsigned bit) noexcept {
225  return bits & bit_set<T>(bit);
226 }
227 
238 constexpr bool is_big_endian()
239 {
240  return endian::native == endian::big;
241 }
242 
253 constexpr bool is_little_endian()
254 {
255  return endian::native == endian::little;
256 }
257 
259 
260 } // namespace gul14
261 
262 #endif
263 
264 // vi:ts=4:sw=4:et
endian
An enum to determine the endianness of multi-byte scalars on the current platform.
Definition: bit_manip.h:80
constexpr bool is_little_endian()
Determine whether this platform uses little-endian (Intel) order for storing multi-byte quantities in...
Definition: bit_manip.h:253
constexpr auto bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition: bit_manip.h:121
constexpr auto bit_flip(T previous, unsigned bit) noexcept -> ReturnT
Flip a bit in an integral value.
Definition: bit_manip.h:199
constexpr bool bit_test(T bits, unsigned bit) noexcept
Test a bit in an integral value.
Definition: bit_manip.h:224
constexpr auto bit_reset(T previous, unsigned bit) noexcept -> ReturnT
Reset a bit in an integral value.
Definition: bit_manip.h:172
constexpr bool is_big_endian()
Determine whether this platform uses big-endian (Motorola) order for storing multi-byte quantities in...
Definition: bit_manip.h:238
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:58
@ native
Native endianness.
@ little
Little-endian (e.g. Intel)
@ big
Big-endian (e.g. Motorola)
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26