A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

Binary

Utility functions for manipulating binary numbers.

Rotate

Rotate the bits in the value left or right.

template <typename T>
T rotate_left(T value)

template <typename T>
T rotate_left(T value, size_t distance)

template <typename T>
T rotate_right(T value)

template <typename T>
T rotate_right(T value, size_t distance)

template <typename T>
T rotate(T value, typename etl::make_signed<size_t>::type distance)

Reverse

Reverse the order of the bits or bytes in the value.

template <typename T>
T reverse_bits(T value)

template <typename T>
T reverse_bytes(T value)

Fold

Fold a binary number down to a set number of bits using XOR.

template <typename TReturn, const size_t NBITS, typename TValue>
TReturn fold_bits(TValue value)

Example:
0xE8C9AACCBC3D9A8F folded down to 20 bits = 0x998E8

uint32_t result = etl::fold_bits<uint32_t, 20>(0xE8C9AACCBC3D9A8F);

Sign extend

Sign extends a binary number.
template <typename TReturn, const size_t NBITS, typename TValue>
TReturn sign_extend(TValue value)

template <typename TReturn, typename TValue>
TReturn sign_extend(TValue value, const size_t NBITS)

Count trailing zeros

Counts the number of trailing zeros in a binary number

template <typename T>
count_trailing_zeros(T value)

First set position

Finds the index of the first set bit from lsb.

template <typename T>
uint_least8_t first_set_bit_position(T value)

Constants


enum binary_constant

An enumeration of 256 binary constants from b00000000 to b11111111
binary.h