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


Utility functions for manipulating binary numbers.


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 bits

Reverse the order of the bits in a value.

uint8_t reverse_bits(uint8_t value)
int8_t reverse_bits(int8_t value)

uint16_t reverse_bits(uint16_t value)
int16_t reverse_bits(int16_t value)

uint32_t reverse_bits(uint32_t value)
int32_t reverse_bits(int32_t value)

uint64_t reverse_bits(uint64_t value)
int64_t reverse_bits(int64_t value)

Reverse bytes

Reverse the order of the bytes in a value.

uint8_t reverse_bytes(uint8_t value)
int8_t reverse_bytes(int8_t value)

uint16_t reverse_bytes(uint16_t value)
int16_t reverse_bytes(int16_t value)

uint32_t reverse_bytes(uint32_t value)
int32_t reverse_bytes(int32_t value)

uint64_t reverse_bytes(uint64_t value)
int64_t reverse_bytes(int64_t value)

Gray to binary

Converts a gray code value to binary.

uint8_t gray_to_binary(uint8_t value)
int8_t gray_to_binary(int8_t value);

uint16_t gray_to_binary(uint16_t value)
int16_t gray_to_binary(int16_t value)

uint32_t gray_to_binary(uint32_t value)
int32_t gray_to_binary(int32_t value)

uint64_t gray_to_binary(uint64_t value)
int64_t gray_to_binary(int64_t value)

Binary to gray

Converts a binary value to the gray code equivalent.

template <typename T>
T binary_to_gray(T value)

Count bits

Counts the number of set bits in a value.

uint_least8_t count_bits(uint8_t value)
uint_least8_t count_bits(int8_t value)

uint_least8_t count_bits(uint16_t value)
uint_least8_t count_bits(int16_t value)

uint_least8_t count_bits(uint32_t value)
uint_least8_t count_bits(int32_t value)

uint_least8_t count_bits(uint64_t value)
uint_least8_t count_bits(int64_t value)


Returns 1 if the parity of the value is odd, 0 if it is even.

uint_least8_t parity(uint8_t value)
uint_least8_t parity(int8_t value)
uint_least8_t parity(uint16_t value)
uint_least8_t parity(int16_t value)
uint_least8_t parity(uint32_t value)
uint_least8_t parity(int32_t value)
uint_least8_t parity(uint64_t value)
uint_least8_t parity(int64_t value)

Max value for N bits

Returns maximum unsigned value a particular number of bits can represent.

template <const size_t NBITS>
struct max_value_for_nbits
value_type The type for the value.
value      The maximum value.


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)

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

uint_least8_t count_trailing_zeros(uint8_t value)
uint_least8_t count_trailing_zeros(int8_t value)
uint_least8_t count_trailing_zeros(uint16_t value)
uint_least8_t count_trailing_zeros(int16_t value)
uint_least8_t count_trailing_zeros(uint32_t value)
uint_least8_t count_trailing_zeros(int32_t value)
uint_least8_t count_trailing_zeros(uint64_t value)
uint_least8_t count_trailing_zeros(int64_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)

First clear position

Finds the index of the first clear bit from lsb.

template <typename T>
uint_least8_t first_clear_bit_position(T value)

First position of bit in the specified state

Finds the index of the first bit in the specified state, from lsb.

template <typename T>
uint_least8_t first_bit_position(bool state, T value)


enum binary_constant
An enumeration of 256 constants from b00000000 to b11111111 (0 to 255)

enum bit_constant
An enumeration of 32 constants from b0 to b31 (1 to 4294967296)

template <const size_t POSITION>
struct bit
value_type The type of the value.
value      The value of the bit at POSITION.