A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove
Support the development
of the ETL

Binary

Utility functions for manipulating binary numbers.

ETL_CONSTEXPR   = constexpr for C++11 or above
ETL_CONSTEXPR14 = constexpr for C++14  or above

____________________________________________________________________________________________________

Rotate

Rotate the bits in the value left or right.

template <typename T>
ETL_CONSTEXPR14 T rotate_left(T value)

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

template <typename T>
ETL_CONSTEXPR14 T rotate_right(T value)

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

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

____________________________________________________________________________________________________

Reverse bits

Reverse the order of the bits in a value.

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

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

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

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

____________________________________________________________________________________________________

Reverse bytes

Reverse the order of the bytes in a value.

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

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

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

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

____________________________________________________________________________________________________

Gray to binary

Converts a gray code value to binary.

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

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

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

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

____________________________________________________________________________________________________

Binary to gray

Converts a binary value to the gray code equivalent.

template <typename T>
ETL_CONSTEXPR T binary_to_gray(T value)

____________________________________________________________________________________________________

Count bits

Counts the number of set bits in a value.

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

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

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

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

____________________________________________________________________________________________________

Parity

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


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

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

template <typename TReturn, const size_t NBITS, typename TValue>
ETL_CONSTEXPR14 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>
ETL_CONSTEXPR14 TReturn sign_extend(TValue value)

Converts an N bit binary number, where bit N-1 is the sign bit, to a signed integral type.
____________________________________________________________________________________________________

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

Converts an N bit binary number, where bit N-1 is the sign bit, and SHIFT is the right shift amount, to a signed integral
type.
____________________________________________________________________________________________________

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

Converts an N bit binary number, where bit N-1 is the sign bit, to a signed integral type.
____________________________________________________________________________________________________

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

Converts an N bit binary number, where bit N-1 is the sign bit, and SHIFT is the right shift amount, to a signed integral
type.

____________________________________________________________________________________________________

Count trailing zeros

Counts the number of trailing zeros in a binary number

ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(uint8_t value)
ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(int8_t value)
ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(uint16_t value)
ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(int16_t value)
ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(uint32_t value)
ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(int32_t value)
ETL_CONSTEXPR14 uint_least8_t count_trailing_zeros(uint64_t value)
ETL_CONSTEXPR14 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>
ETL_CONSTEXPR14 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>
ETL_CONSTEXPR14 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>
ETL_CONSTEXPR14 uint_least8_t first_bit_position(bool state, T value)

____________________________________________________________________________________________________

Fill a binary value with a pattern

Fills a value of the specified width with a repeating binary pattern.

Run time

template <typename TResult, typename TValue>
ETL_CONSTEXPR TResult binary_fill(TValue value)

Generate 0x12121212
etl::binary_fill<uint32_t>(uint8_t(0x12));

Partial compile time

template <typename TResult, typename TValue, const TValue N>
ETL_CONSTEXPR TResult binary_fill(TValue value)

Generate 0x12121212
etl::binary_fill<uint32_t, uint8_t, 0x12>();

____________________________________________________________________________________________________

Check a binary value for a zero byte

Checks to see if a value contains a byte of value zero.

Run time

template <typename TValue>
ETL_CONSTEXPR14 bool has_zero_byte(const TValue value)

etl::has_zero_byte(uint32_t(0x01234567)) == false
etl::has_zero_byte(uint32_t(0x01230067)) == true

____________________________________________________________________________________________________

Check a binary value for a particular value byte

Checks to see if a value contains a byte of a particular value.

Run time


template <typename TValue>
ETL_CONSTEXPR14 bool has_byte_n(TValue value, uint8_t n)

etl::has_byte_n(uint32_t(0x01234567), 0x12) == false
etl::has_byte_n(uint32_t(0x01234567), 0x45) == true

Partial compile time


template <typename TValue, const TValue N>
ETL_CONSTEXPR14 bool has_byte_n(TValue value)

etl::has_byte_n<0x12>(uint32_t(0x01234567)) == false
etl::has_byte_n<0x45>(uint32_t(0x01234567)) == true

____________________________________________________________________________________________________

Merge two values

Merges two binary values according to a mask.
Bits set in the mask select bits in the first value, clear bits select those in the second.

template <typename T>
ETL_CONSTEXPR T binary_merge(const T first, const T second, const T mask)

uint8_t first  = 0x12;
uint8_t second = 0x34;

const uint8_t mask = 0xF0;

etl::binary_merge(first, second, mask) Equals 0x14

template <typename T, const T MASK>
ETL_CONSTEXPR T binary_merge(const T first, const T second)

uint8_t first  = 0x12;
uint8_t second = 0x34;

const uint8_t mask = 0xF0;

etl::binary_merge<uint8_t, mask>(first, second) Equals 0x14

____________________________________________________________________________________________________

Interleave two values

Interleaves two values such that bits abcd and efgh will result in eafbgchd.

ETL_CONSTEXPR14 uint16_t binary_interleave(uint8_t  first, uint8_t  second);
ETL_CONSTEXPR14 int16_t binary_interleave(int8_t    first, int8_t   second);
ETL_CONSTEXPR14 uint32_t binary_interleave(uint16_t first, uint16_t second);
ETL_CONSTEXPR14 int32_t binary_interleave(int16_t   first, int16_t  second);
ETL_CONSTEXPR14 uint64_t binary_interleave(uint32_t first, uint32_t second);
ETL_CONSTEXPR14 int64_t binary_interleave(int32_t   first, int32_t  second);

____________________________________________________________________________________________________

Odd / Even

Determines the odd or evenness of a value.

ETL_CONSTEXPR bool is_odd(const unsigned char value);
ETL_CONSTEXPR bool is_odd(const char value);

ETL_CONSTEXPR bool is_odd(const unsigned short value);
ETL_CONSTEXPR bool is_odd(const short value);

ETL_CONSTEXPR bool is_odd(const unsigned int value);
ETL_CONSTEXPR bool is_odd(const int value);

ETL_CONSTEXPR bool is_odd(const unsigned long value);
ETL_CONSTEXPR bool is_odd(const long value);

ETL_CONSTEXPR bool is_odd(const unsigned long long value);
ETL_CONSTEXPR bool is_odd(const long long value);


ETL_CONSTEXPR bool is_even(const unsigned char value);
ETL_CONSTEXPR bool is_even(const char value);

ETL_CONSTEXPR bool is_even(const unsigned short value);
ETL_CONSTEXPR bool is_even(const short value);

ETL_CONSTEXPR bool is_even(const unsigned int value);
ETL_CONSTEXPR bool is_even(const int value);

ETL_CONSTEXPR bool is_even(const unsigned long value);
ETL_CONSTEXPR bool is_even(const long value);

ETL_CONSTEXPR bool is_even(const unsigned long long value);
ETL_CONSTEXPR bool is_even(const long long value);

____________________________________________________________________________________________________

Constants

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.
binary.h