Antkeeper  0.0.1
Functions
bit Namespace Reference

Bitwise math. More...

Functions

template<class T >
constexpr T compress (T x) noexcept
 Compresses the even bits of a value into the lower half, then clears the upper half. More...
 
template<class T >
constexpr int count (T x) noexcept
 Returns the number of set bits in a value, known as a population count or Hamming weight. More...
 
template<class T >
constexpr T crossover (T a, T b, int i) noexcept
 Performs a single-point crossover between two values. More...
 
template<class T >
constexpr T crossover_n (T a, T b, T mask) noexcept
 Performs an n-point crossover between two values. More...
 
template<class T >
constexpr T deposit (T x, T mask) noexcept
 Reads bits from the least significant bits of a value and returns them in the positions marked by a mask. More...
 
template<class T >
constexpr T desegregate (T x) noexcept
 Interleaves bits of the lower and upper halves of a value. More...
 
template<class T >
constexpr int difference (T x, T y) noexcept
 Returns the number of differing bits between two values, known as Hamming distance. More...
 
template<class T >
constexpr T expand (T x) noexcept
 Moves bits from the lower half of a value to occupy all even bits, and clears all odd bits. More...
 
template<class T >
constexpr T extract (T x, T mask) noexcept
 Reads bits from a value in the positions marked by a mask and returns them in the least significant bits. More...
 
template<class T >
constexpr T flip (T x, int i) noexcept
 Flips a single bit in a value. More...
 
template<class T , class U = T>
constexpr U interleave (T a, T b) noexcept
 Returns a value with even bits containing the first value's lower half, and odd bits containing the second value's lower half. More...
 
template<class T >
constexpr T merge (T a, T b, T mask) noexcept
 Merges the bits of two values using a bit mask. More...
 
template<class T >
constexpr T parity (T x) noexcept
 Returns the parity of a value. More...
 
template<class T >
constexpr T segregate (T x) noexcept
 Segregates the odd and even bits of a value. More...
 
template<class T >
constexpr T swap_adjacent (T x) noexcept
 Swaps the each odd bit with its following even bit. More...
 
std::uint16_t swap16 (std::uint16_t x) noexcept
 Swaps the byte order of an unsigned 16-bit number. More...
 
std::int16_t swap16 (std::int16_t x) noexcept
 Swaps the byte order of a signed 16-bit number. More...
 
std::uint32_t swap32 (std::uint32_t x) noexcept
 Swaps the byte order of an unsigned 32-bit number. More...
 
std::int32_t swap32 (std::int32_t x) noexcept
 Swaps the byte order of a signed 32-bit number. More...
 
std::uint64_t swap64 (std::uint64_t x) noexcept
 Swaps the byte order of an unsigned 64-bit number. More...
 
std::int64_t swap64 (std::int64_t x) noexcept
 Swaps the byte order of a signed 64-bit number. More...
 

Detailed Description

Bitwise math.

Function Documentation

◆ compress()

template<class T >
constexpr T bit::compress ( x)
constexprnoexcept

Compresses the even bits of a value into the lower half, then clears the upper half.

Parameters
xValue to compress.
Returns
Compressed value.

Definition at line 193 of file bit-math.hpp.

◆ count()

template<class T >
constexpr int bit::count ( x)
constexprnoexcept

Returns the number of set bits in a value, known as a population count or Hamming weight.

Parameters
xValue to count.
Returns
Number of set bits in x.

Definition at line 211 of file bit-math.hpp.

◆ crossover()

template<class T >
constexpr T bit::crossover ( a,
b,
int  i 
)
inlineconstexprnoexcept

Performs a single-point crossover between two values.

Parameters
aFirst value.
bSecond value.
iIndex of the crossover point.
Returns
Crossed over value.

Definition at line 220 of file bit-math.hpp.

◆ crossover_n()

template<class T >
constexpr T bit::crossover_n ( a,
b,
mask 
)
constexprnoexcept

Performs an n-point crossover between two values.

Parameters
aFirst value.
bSecond value.
maskBit mask with set bits marking crossover points.
Returns
Crossed over value.

Definition at line 227 of file bit-math.hpp.

◆ deposit()

template<class T >
constexpr T bit::deposit ( x,
mask 
)
constexprnoexcept

Reads bits from the least significant bits of a value and returns them in the positions marked by a mask.

Parameters
xValue from which bits should be read.
maskBit mask indicating where bits should be deposited.
Returns
Bits from the least significant bits of x in the positions marked by mask.

Definition at line 265 of file bit-math.hpp.

◆ desegregate()

template<class T >
constexpr T bit::desegregate ( x)
inlineconstexprnoexcept

Interleaves bits of the lower and upper halves of a value.

Parameters
xValue to desegregate.
Returns
Value with bits from the upper half of x interleaved with bits from the lower half.

Definition at line 241 of file bit-math.hpp.

◆ difference()

template<class T >
constexpr int bit::difference ( x,
y 
)
inlineconstexprnoexcept

Returns the number of differing bits between two values, known as Hamming distance.

Parameters
xFirst value.
ySecond value.
Returns
Hamming distance between @px and y.

Definition at line 280 of file bit-math.hpp.

◆ expand()

template<class T >
constexpr T bit::expand ( x)
constexprnoexcept

Moves bits from the lower half of a value to occupy all even bits, and clears all odd bits.

Parameters
xValue to expand.
Returns
Expanded value.

Definition at line 247 of file bit-math.hpp.

◆ extract()

template<class T >
constexpr T bit::extract ( x,
mask 
)
constexprnoexcept

Reads bits from a value in the positions marked by a mask and returns them in the least significant bits.

Parameters
xValue from which bits should be read.
maskBit mask indicating which bits to extract.
Returns
Bits of x from the positions marked by mask in the least significant bits.

Definition at line 286 of file bit-math.hpp.

◆ flip()

template<class T >
constexpr T bit::flip ( x,
int  i 
)
inlineconstexprnoexcept

Flips a single bit in a value.

Parameters
xValue to modify.
iIndex of the bit to flip.
Returns
Copy of x with one bit flipped.

Definition at line 301 of file bit-math.hpp.

◆ interleave()

template<class T , class U = T>
constexpr U bit::interleave ( a,
b 
)
inlineconstexprnoexcept

Returns a value with even bits containing the first value's lower half, and odd bits containing the second value's lower half.

Parameters
aFirst value.
bSecond value.
Returns
Value with bits from the lower halves of a and b interleaved.

Definition at line 307 of file bit-math.hpp.

◆ merge()

template<class T >
constexpr T bit::merge ( a,
b,
mask 
)
inlineconstexprnoexcept

Merges the bits of two values using a bit mask.

Parameters
aFirst value.
bSecond value.
maskBit mask with zeros where bits from a should be used, and ones where bits from b should be used.
Returns
Merged value.

Definition at line 313 of file bit-math.hpp.

◆ parity()

template<class T >
constexpr T bit::parity ( x)
constexprnoexcept

Returns the parity of a value.

Parameters
xValue to test.
Returns
1 if an odd number of bits are set, 0 otherwise.

Definition at line 319 of file bit-math.hpp.

◆ segregate()

template<class T >
constexpr T bit::segregate ( x)
constexprnoexcept

Segregates the odd and even bits of a value.

Parameters
xValue to segregate.
Returns
Value with even bits of x in the lower half, and odd bits in the upper half.

Definition at line 334 of file bit-math.hpp.

◆ swap16() [1/2]

std::int16_t bit::swap16 ( std::int16_t  x)
noexcept

Swaps the byte order of a signed 16-bit number.

Definition at line 353 of file bit-math.hpp.

◆ swap16() [2/2]

std::uint16_t bit::swap16 ( std::uint16_t  x)
noexcept

Swaps the byte order of an unsigned 16-bit number.

Definition at line 348 of file bit-math.hpp.

◆ swap32() [1/2]

std::int32_t bit::swap32 ( std::int32_t  x)
noexcept

Swaps the byte order of a signed 32-bit number.

Definition at line 364 of file bit-math.hpp.

◆ swap32() [2/2]

std::uint32_t bit::swap32 ( std::uint32_t  x)
noexcept

Swaps the byte order of an unsigned 32-bit number.

Definition at line 358 of file bit-math.hpp.

◆ swap64() [1/2]

std::int64_t bit::swap64 ( std::int64_t  x)
noexcept

Swaps the byte order of a signed 64-bit number.

Definition at line 377 of file bit-math.hpp.

◆ swap64() [2/2]

std::uint64_t bit::swap64 ( std::uint64_t  x)
noexcept

Swaps the byte order of an unsigned 64-bit number.

Definition at line 370 of file bit-math.hpp.

◆ swap_adjacent()

template<class T >
constexpr T bit::swap_adjacent ( x)
constexprnoexcept

Swaps the each odd bit with its following even bit.

Parameters
xValue in which adjacent bits should be swap.
Returns
Copy of x with adjacent bits swapped.

Definition at line 343 of file bit-math.hpp.