header_utils
Loading...
Searching...
No Matches
Bits

Utilities to manipulate bits in integral values. More...

Concepts

concept  ghassanpl::bit_integral
 Whether a type is integral (but not a bool).
concept  ghassanpl::bit_castable
 Whether two types can be safely bit_cast between each other.

Classes

struct  ghassanpl::bit_view< INTEGER_TYPE >
 A view over an integral value allowing for iteration and modification of its individual bits. More...
struct  ghassanpl::bit_reference< VALUE_TYPE, BIT_NUM >
 Models a reference to a specific bit in a variable. Can be a statically-defined bit number (BIT_NUM != dynamic_bit_number). More...

Typedefs

template<size_t N>
using ghassanpl::uintN_t
 An unsigned integer type for the given PoT bit size.
template<typename T>
using ghassanpl::uint_sizeof_t
template<size_t N>
using ghassanpl::intN_t
 An integer type for the given PoT bit size.
template<typename T>
using ghassanpl::int_sizeof_t
template<bool SIGNED, size_t N>
using ghassanpl::sintN_t
 A signed integer type for the given PoT bit size.

Functions

template<std::ranges::contiguous_range RANGE_TYPE>
 ghassanpl::bit_view (RANGE_TYPE &T) -> bit_view< std::ranges::range_value_t< std::decay_t< decltype(T)> > >
template<std::ranges::contiguous_range RANGE_TYPE>
 ghassanpl::bit_view (RANGE_TYPE const &T) -> bit_view< std::ranges::range_value_t< std::decay_t< decltype(T)> > const >
template<std::ranges::contiguous_range RANGE_TYPE>
auto ghassanpl::make_bit_reference (RANGE_TYPE &&range, size_t bit_num)
template<size_t BIT_NUM, std::ranges::contiguous_range RANGE_TYPE>
auto ghassanpl::make_bit_reference (RANGE_TYPE &&range)
template<bit_integral VALUE_TYPE>
auto ghassanpl::make_bit_reference (VALUE_TYPE &value, size_t bit_num)
template<size_t BIT_NUM, bit_integral VALUE_TYPE>
auto ghassanpl::make_bit_reference (VALUE_TYPE &value)
template<size_t BEGIN, size_t END, bit_integral T>
constexpr T ghassanpl::extract_bits (T value)
 Returns just the bits between BEGIN and END in value.
template<size_t... RANGES, bit_integral T>
constexpr auto ghassanpl::split_bit_ranges (T value)
template<size_t N, bit_integral T>
constexpr bool ghassanpl::bit_at (T bits)
template<bit_integral... BIT_TYPES>
constexpr auto ghassanpl::cat_bits (BIT_TYPES... bits)
 Creates a new integral value by concatenating the given bit values.
template<size_t... BIT_COUNT, bit_integral... BIT_TYPES>
constexpr auto ghassanpl::cat_bits_n (BIT_TYPES... values)
 Creates a new integral value by concatenating the given bit values, each truncated to the given bit count.
template<size_t N, bit_integral T>
constexpr T ghassanpl::n_most_significant (T value)
template<size_t N, bit_integral T>
constexpr T ghassanpl::n_least_significant (T value)
template<bit_integral T>
constexpr auto ghassanpl::most_significant_half (T v) noexcept
 Returns an integer with the N/2 most significant bits of the given N-bit integer.
template<bit_integral T>
constexpr auto ghassanpl::least_significant_half (T v) noexcept
 Returns an integer with the N/2 least significant bits of the given N-bit integer.
template<bit_integral T>
constexpr auto ghassanpl::split_bits (T v) noexcept
 Returns a pair of integers with the N/2 most significant and least significant bits of the given N-bit integer.
template<std::unsigned_integral T>
constexpr T ghassanpl::bit_reverse (T x) noexcept
 Reverses the bits of x so that the least significant bit becomes the most significant.
template<std::unsigned_integral T>
constexpr T ghassanpl::bit_repeat (T x, int length) noexcept(false)
 Repeats a pattern stored in the least significant length bits of x, as many times as fits into x.
template<std::unsigned_integral T>
constexpr T ghassanpl::bit_compress (T x, T m) noexcept
 For each one-bit in m, the corresponding bit in x is taken and packed contiguously into the result, starting with the least significant result bit.
template<std::unsigned_integral T>
constexpr T ghassanpl::bit_expand (T x, T m) noexcept
 For each one-bit in m, a bit from x, starting with the least significant bit is taken and shifted into the corresponding position of the m bit.

Variables

template<typename T>
constexpr auto ghassanpl::bit_count
 Equal to the number of bits in the type.
constexpr uint64_t ghassanpl::all_bits
 A value of type uint64_t with all bits set.
template<size_t BEGIN, size_t END>
constexpr uint64_t ghassanpl::bit_mask_v
 Value with bits between BEGIN and END (exclusive) set.
template<bit_integral FOR>
constexpr uint64_t ghassanpl::bit_mask_for_v
 Value with all bits available for the FOR type set (e.g. first 8 bits for uint8_t will be set, etc.).
constexpr size_t ghassanpl::dynamic_bit_number
 Used to specify that a ghassanpl::bit_reference references a bit number given at runtime.

Endianness

template<bit_integral B>
constexpr B ghassanpl::to_big_endian (B val) noexcept
 Returns val in its big-endian representation.
template<bit_integral B>
constexpr B ghassanpl::to_little_endian (B val) noexcept
 Returns val in its little-endian representation.
template<bit_integral B>
constexpr B ghassanpl::from_big_endian (B val) noexcept
 Returns val (in big-endian representation) converted into native representation.
template<bit_integral B>
constexpr B ghassanpl::from_little_endian (B val) noexcept
 Returns val (in little-endian representation) converted into native representation.
template<std::endian ENDIANNESS, bit_integral B>
constexpr B ghassanpl::to_endian (B val) noexcept
 Returns val in its ENDIANNESS representation.
template<bit_integral B>
constexpr B ghassanpl::to_endian (B val, std::endian endianness) noexcept
 Returns val in its endianness representation.
template<std::endian ENDIANNESS, bit_integral B>
constexpr B ghassanpl::from_endian (B val) noexcept
 Returns val (in ENDIANNESS representation) converted into native representation.
template<bit_integral B>
constexpr B ghassanpl::from_endian (B val, std::endian endianness) noexcept
 Returns val (in endianness representation) converted into native representation.

Detailed Description

Utilities to manipulate bits in integral values.

Types and functions for retrieving and manipulating bits in integral values

Typedef Documentation

◆ int_sizeof_t

template<typename T>
using ghassanpl::int_sizeof_t

Definition at line 116 of file bits.h.

◆ intN_t

template<size_t N>
using ghassanpl::intN_t

An integer type for the given PoT bit size.

Definition at line 113 of file bits.h.

◆ sintN_t

template<bool SIGNED, size_t N>
using ghassanpl::sintN_t

A signed integer type for the given PoT bit size.

Definition at line 128 of file bits.h.

◆ uint_sizeof_t

template<typename T>
using ghassanpl::uint_sizeof_t

Definition at line 100 of file bits.h.

◆ uintN_t

template<size_t N>
using ghassanpl::uintN_t

An unsigned integer type for the given PoT bit size.

Definition at line 97 of file bits.h.

Function Documentation

◆ bit_at()

template<size_t N, bit_integral T>
bool ghassanpl::bit_at ( T bits)
constexpr

Definition at line 77 of file bits.h.

◆ bit_compress()

template<std::unsigned_integral T>
T ghassanpl::bit_compress ( T x,
T m )
constexprnoexcept

For each one-bit in m, the corresponding bit in x is taken and packed contiguously into the result, starting with the least significant result bit.

Definition at line 217 of file bits.h.

◆ bit_expand()

template<std::unsigned_integral T>
T ghassanpl::bit_expand ( T x,
T m )
constexprnoexcept

For each one-bit in m, a bit from x, starting with the least significant bit is taken and shifted into the corresponding position of the m bit.

Definition at line 230 of file bits.h.

◆ bit_repeat()

template<std::unsigned_integral T>
T ghassanpl::bit_repeat ( T x,
int length )
constexpr

Repeats a pattern stored in the least significant length bits of x, as many times as fits into x.

Definition at line 206 of file bits.h.

◆ bit_reverse()

template<std::unsigned_integral T>
T ghassanpl::bit_reverse ( T x)
constexprnoexcept

Reverses the bits of x so that the least significant bit becomes the most significant.

Definition at line 193 of file bits.h.

◆ cat_bits()

template<bit_integral... BIT_TYPES>
auto ghassanpl::cat_bits ( BIT_TYPES... bits)
constexpr

Creates a new integral value by concatenating the given bit values.

Definition at line 132 of file bits.h.

◆ cat_bits_n()

template<size_t... BIT_COUNT, bit_integral... BIT_TYPES>
auto ghassanpl::cat_bits_n ( BIT_TYPES... values)
constexpr

Creates a new integral value by concatenating the given bit values, each truncated to the given bit count.

Definition at line 146 of file bits.h.

◆ extract_bits()

template<size_t BEGIN, size_t END, bit_integral T>
T ghassanpl::extract_bits ( T value)
constexpr

Returns just the bits between BEGIN and END in value.

Definition at line 56 of file bits.h.

◆ from_big_endian()

template<bit_integral B>
B ghassanpl::from_big_endian ( B val)
nodiscardconstexprnoexcept

Returns val (in big-endian representation) converted into native representation.

Definition at line 254 of file bits.h.

◆ from_endian() [1/2]

template<std::endian ENDIANNESS, bit_integral B>
B ghassanpl::from_endian ( B val)
nodiscardconstexprnoexcept

Returns val (in ENDIANNESS representation) converted into native representation.

Definition at line 270 of file bits.h.

◆ from_endian() [2/2]

template<bit_integral B>
B ghassanpl::from_endian ( B val,
std::endian endianness )
nodiscardconstexprnoexcept

Returns val (in endianness representation) converted into native representation.

Definition at line 274 of file bits.h.

◆ from_little_endian()

template<bit_integral B>
B ghassanpl::from_little_endian ( B val)
nodiscardconstexprnoexcept

Returns val (in little-endian representation) converted into native representation.

Definition at line 258 of file bits.h.

◆ least_significant_half()

template<bit_integral T>
auto ghassanpl::least_significant_half ( T v)
nodiscardconstexprnoexcept

Returns an integer with the N/2 least significant bits of the given N-bit integer.

Definition at line 177 of file bits.h.

◆ make_bit_reference() [1/4]

template<size_t BIT_NUM, std::ranges::contiguous_range RANGE_TYPE>
auto ghassanpl::make_bit_reference ( RANGE_TYPE && range)
nodiscard

Definition at line 223 of file bit_view.h.

◆ make_bit_reference() [2/4]

template<std::ranges::contiguous_range RANGE_TYPE>
auto ghassanpl::make_bit_reference ( RANGE_TYPE && range,
size_t bit_num )
nodiscard

Definition at line 216 of file bit_view.h.

◆ make_bit_reference() [3/4]

template<size_t BIT_NUM, bit_integral VALUE_TYPE>
auto ghassanpl::make_bit_reference ( VALUE_TYPE & value)
nodiscard

Definition at line 236 of file bit_view.h.

◆ make_bit_reference() [4/4]

template<bit_integral VALUE_TYPE>
auto ghassanpl::make_bit_reference ( VALUE_TYPE & value,
size_t bit_num )
nodiscard

Definition at line 230 of file bit_view.h.

◆ most_significant_half()

template<bit_integral T>
auto ghassanpl::most_significant_half ( T v)
nodiscardconstexprnoexcept

Returns an integer with the N/2 most significant bits of the given N-bit integer.

Definition at line 168 of file bits.h.

◆ n_least_significant()

template<size_t N, bit_integral T>
T ghassanpl::n_least_significant ( T value)
constexpr

Definition at line 164 of file bits.h.

◆ n_most_significant()

template<size_t N, bit_integral T>
T ghassanpl::n_most_significant ( T value)
constexpr

Definition at line 161 of file bits.h.

◆ split_bit_ranges()

template<size_t... RANGES, bit_integral T>
auto ghassanpl::split_bit_ranges ( T value)
constexpr

Definition at line 63 of file bits.h.

◆ split_bits()

template<bit_integral T>
auto ghassanpl::split_bits ( T v)
nodiscardconstexprnoexcept

Returns a pair of integers with the N/2 most significant and least significant bits of the given N-bit integer.

Definition at line 186 of file bits.h.

◆ to_big_endian()

template<bit_integral B>
B ghassanpl::to_big_endian ( B val)
nodiscardconstexprnoexcept

Returns val in its big-endian representation.

Definition at line 246 of file bits.h.

◆ to_endian() [1/2]

template<std::endian ENDIANNESS, bit_integral B>
B ghassanpl::to_endian ( B val)
nodiscardconstexprnoexcept

Returns val in its ENDIANNESS representation.

Definition at line 262 of file bits.h.

◆ to_endian() [2/2]

template<bit_integral B>
B ghassanpl::to_endian ( B val,
std::endian endianness )
nodiscardconstexprnoexcept

Returns val in its endianness representation.

Definition at line 266 of file bits.h.

◆ to_little_endian()

template<bit_integral B>
B ghassanpl::to_little_endian ( B val)
nodiscardconstexprnoexcept

Returns val in its little-endian representation.

Definition at line 250 of file bits.h.

Variable Documentation

◆ all_bits

uint64_t ghassanpl::all_bits
inlineconstexpr

A value of type uint64_t with all bits set.

Definition at line 39 of file bits.h.

◆ bit_count

template<typename T>
auto ghassanpl::bit_count
inlineconstexpr

Equal to the number of bits in the type.

Definition at line 36 of file bits.h.

◆ bit_mask_for_v

template<bit_integral FOR>
uint64_t ghassanpl::bit_mask_for_v
inlineconstexpr

Value with all bits available for the FOR type set (e.g. first 8 bits for uint8_t will be set, etc.).

Definition at line 48 of file bits.h.

◆ bit_mask_v

template<size_t BEGIN, size_t END>
uint64_t ghassanpl::bit_mask_v
inlineconstexpr

Value with bits between BEGIN and END (exclusive) set.

Definition at line 44 of file bits.h.

◆ dynamic_bit_number

size_t ghassanpl::dynamic_bit_number
inlineconstexpr

Used to specify that a ghassanpl::bit_reference references a bit number given at runtime.

Definition at line 279 of file bits.h.