General Utility Library for C++14  2.12
Typedefs | Enumerations | Functions
gul14/bit_manip.h

Detailed Description

Bit manipulation and testing, endianness.

Typedefs

template<typename T >
using gul14::BitFunctionReturnType = std::enable_if_t< std::is_integral< T >::value and not std::is_same< std::decay_t< T >, bool >::value, std::decay_t< T > >
 Return type of the bit manipulation functions. More...
 

Enumerations

enum class  gul14::endian { gul14::little , gul14::big , gul14::native }
 An enum to determine the endianness of multi-byte scalars on the current platform. More...
 

Functions

template<typename T = unsigned, typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_set (unsigned bit) noexcept -> ReturnT
 Set a bit in an integral type. More...
 
template<typename T , typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_set (T previous, unsigned bit) noexcept -> ReturnT
 Set a bit in an integral value. More...
 
template<typename T , typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_reset (T previous, unsigned bit) noexcept -> ReturnT
 Reset a bit in an integral value. More...
 
template<typename T , typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_flip (T previous, unsigned bit) noexcept -> ReturnT
 Flip a bit in an integral value. More...
 
template<typename T >
constexpr bool gul14::bit_test (T bits, unsigned bit) noexcept
 Test a bit in an integral value. More...
 
constexpr bool gul14::is_big_endian ()
 Determine whether this platform uses big-endian (Motorola) order for storing multi-byte quantities in memory. More...
 
constexpr bool gul14::is_little_endian ()
 Determine whether this platform uses little-endian (Intel) order for storing multi-byte quantities in memory. More...
 

Typedef Documentation

◆ BitFunctionReturnType

template<typename T >
using gul14::BitFunctionReturnType = typedef std::enable_if_t< std::is_integral<T>::value and not std::is_same<std::decay_t<T>, bool>::value, std::decay_t<T> >

Return type of the bit manipulation functions.

We want to SFINAE out of all types except integers. Because bools counts as integer type and we don't want that we specifically exclude it here.

The type is then simply the decayed input type, to get rid of const or volatile specifiers.

Template Parameters
TType specified/deduced by bit_*() user

Enumeration Type Documentation

◆ endian

enum gul14::endian
strong

An enum to determine the endianness of multi-byte scalars on the current platform.

In big-endian (Motorola) order, the most significant byte is stored first, followed by the other bytes in order of decreasing significance. Little-endian platforms (e.g. Intel) store the bytes in the opposite order. There are also (historical) platforms which do not conform to either of these conventions.

std::cout << "This is a big-endian machine!\n";
@ native
Native endianness.
@ big
Big-endian (e.g. Motorola)

This is a backport of std::endian from C++20.

See also
is_big_endian(), is_little_endian()
Since
GUL version 2.10
Enumerator
little 

Little-endian (e.g. Intel)

big 

Big-endian (e.g. Motorola)

native 

Native endianness.

Function Documentation

◆ bit_flip()

template<typename T , typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_flip ( previous,
unsigned  bit 
) -> ReturnT
inlineconstexprnoexcept

Flip a bit in an integral value.

Flips the bit number bit in the existing value previous. This inverts the state of the bit: setting the bit if it was previously not set and resetting the bit if it was previously set.

When bit is greater or equal to the number of bits in type T, std::abort() is called (via assert()).

Parameters
previousExisting integral value where the bit shall be modified
bitNumber of the bit that is to be modified (LSB == 0)
Returns
New integral value with the bit modified.
Template Parameters
TType of the bit-holding integral value.
Since
GUL version 1.8
See also
bit_set(bit), bit_set(previous, bit), bit_reset(previous, bit), bit_test(bits, bit)

◆ bit_reset()

template<typename T , typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_reset ( previous,
unsigned  bit 
) -> ReturnT
inlineconstexprnoexcept

Reset a bit in an integral value.

Reset (clear) the bit number bit in the existing value previous.

When bit is greater or equal to the number of bits in type T, std::abort() is called (via assert()).

Parameters
previousExisting integral value where the bit shall be modified
bitNumber of the bit that is to be modified (LSB == 0)
Returns
New integral value with the bit modified.
Template Parameters
TType of the bit-holding integral value.
Since
GUL version 1.8
See also
bit_set(bit), bit_set(previous, bit), bit_flip(previous, bit), bit_test(bits, bit)

◆ bit_set() [1/2]

template<typename T , typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_set ( previous,
unsigned  bit 
) -> ReturnT
inlineconstexprnoexcept

Set a bit in an integral value.

Set the bit number bit in the existing value previous.

When bit is greater or equal to the number of bits in type T, std::abort() is called (via assert()).

Parameters
previousExisting integral value where the bit shall be modified
bitNumber of the bit that is to be modified (LSB == 0)
Returns
New integral value with the bit modified.
Template Parameters
TType of the bit-holding integral value.
Since
GUL version 1.8
See also
bit_set(bit), bit_reset(previous, bit), bit_flip(previous, bit), bit_test(bits, bit)

◆ bit_set() [2/2]

template<typename T = unsigned, typename ReturnT = BitFunctionReturnType<T>>
constexpr auto gul14::bit_set ( unsigned  bit) -> ReturnT
inlineconstexprnoexcept

Set a bit in an integral type.

Return an integral value of type T where the bit number bit and only that bit is set.

When bit is greater or equal to the number of bits in type T, std::abort() is called (via assert()).

Parameters
bitNumber of the bit that is to be set (LSB == 0)
Returns
Integral value with the bit set.
Template Parameters
TType of the bit-holding integral value.
Note
If no type T is specified an unsigned int will be returned.
Since
GUL version 1.8
See also
bit_set(previous, bit), bit_reset(previous, bit), bit_flip(previous, bit), bit_test(bits, bit)

◆ bit_test()

template<typename T >
constexpr bool gul14::bit_test ( bits,
unsigned  bit 
)
inlineconstexprnoexcept

Test a bit in an integral value.

Test the bit number bit in the existing value bits.

When bit is greater or equal to the number of bits in type T, std::abort() is called (via assert()).

Parameters
bitsIntegral value where the bit shall be tested
bitNumber of the bit that is to be modified (LSB == 0)
Returns
True or false
Template Parameters
TType of the bit-holding integral value.
Since
GUL version 1.8
See also
bit_set(bit), bit_set(previous, bit), bit_reset(previous, bit), bit_flip(previous, bit)

◆ is_big_endian()

constexpr bool gul14::is_big_endian ( )
constexpr

Determine whether this platform uses big-endian (Motorola) order for storing multi-byte quantities in memory.

In big-endian order, the most significant byte is stored first, followed by the other bytes in order of decreasing significance.

See also
is_little_endian(), gul14::endian
Since
GUL version 2.10

References gul14::big, and gul14::native.

◆ is_little_endian()

constexpr bool gul14::is_little_endian ( )
constexpr

Determine whether this platform uses little-endian (Intel) order for storing multi-byte quantities in memory.

In little-endian order, the least significant byte is stored first, followed by the other bytes in order of increasing significance.

See also
is_big_endian(), gul14::endian
Since
GUL version 2.10

References gul14::little, and gul14::native.