General Utility Library for C++17 26.5.0
bit_manip.h
Go to the documentation of this file.
1
27#ifndef GUL17_BIT_MANIP_H_
28#define GUL17_BIT_MANIP_H_
29
30#include <cassert>
31#include <type_traits>
32
33#include "gul17/internal.h"
34
35namespace gul17 {
36
55template <typename T>
57 std::enable_if_t<
58 std::is_integral<T>::value
59 and not std::is_same<std::decay_t<T>, bool>::value,
60 std::decay_t<T>
61 >;
62
82enum class endian
83{
84#if defined(__BYTE_ORDER__)
85 little = __ORDER_LITTLE_ENDIAN__,
86 big = __ORDER_BIG_ENDIAN__,
87 native = __BYTE_ORDER__
88#elif defined(_MSC_VER) && !defined(__clang__)
89 little = 0,
90 big = 1,
92#else
93 #error "Don't know how to determine machine endianness on this compiler"
94 // Just for Doxygen:
95 little,
96 big,
97 native
98#endif
99};
100
123template <typename T = unsigned, typename ReturnT = BitFunctionReturnType<T>>
124auto constexpr inline bit_set(unsigned bit) noexcept -> ReturnT {
125 assert(bit < sizeof(T) * 8);
126 return static_cast<ReturnT>(std::make_unsigned_t<T>{1u} << bit);
127}
128
149template <typename T, typename ReturnT = BitFunctionReturnType<T>>
150auto constexpr inline bit_set(T previous, unsigned bit) noexcept -> ReturnT {
151 return previous | bit_set<T>(bit);
152}
153
174template <typename T, typename ReturnT = BitFunctionReturnType<T>>
175auto constexpr inline bit_reset(T previous, unsigned bit) noexcept -> ReturnT {
176 return static_cast<ReturnT>(previous & ~bit_set<T>(bit));
177}
178
201template <typename T, typename ReturnT = BitFunctionReturnType<T>>
202auto constexpr inline bit_flip(T previous, unsigned bit) noexcept -> ReturnT {
203 return previous ^ bit_set<T>(bit);
204}
205
226template <typename T>
227bool constexpr inline bit_test(T bits, unsigned bit) noexcept {
228 return bits & bit_set<T>(bit);
229}
230
241constexpr bool is_big_endian()
242{
243 return endian::native == endian::big;
244}
245
256constexpr bool is_little_endian()
257{
259}
260
262
263} // namespace gul17
264
265#endif
266
267// vi:ts=4:sw=4:et
constexpr bool is_little_endian()
Determine whether this platform uses little-endian (Intel) order for storing multi-byte quantities in...
Definition bit_manip.h:256
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
bool constexpr bit_test(T bits, unsigned bit) noexcept
Test a bit in an integral value.
Definition bit_manip.h:227
constexpr bool is_big_endian()
Determine whether this platform uses big-endian (Motorola) order for storing multi-byte quantities in...
Definition bit_manip.h:241
endian
An enum to determine the endianness of multi-byte scalars on the current platform.
Definition bit_manip.h:83
auto constexpr bit_flip(T previous, unsigned bit) noexcept -> ReturnT
Flip a bit in an integral value.
Definition bit_manip.h:202
auto constexpr bit_reset(T previous, unsigned bit) noexcept -> ReturnT
Reset a bit in an integral value.
Definition bit_manip.h:175
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:61
@ native
Native endianness.
@ little
Little-endian (e.g. Intel)
@ big
Big-endian (e.g. Motorola)
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29