tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches

Bit manipulation functions. More...

Enumerations

enum struct  endian { little = __ORDER_LITTLE_ENDIAN__ , big = __ORDER_BIG_ENDIAN__ , native = __BYTE_ORDER__ }
 Indicates the endianness of all scalar types. If all scalar types are little-endian, endian::native equals endian::little. If all scalar types are big-endian, endian::native equals endian::big. More...
 

Functions

template<typename To, typename From>
requires detail::bitcastable<To, From>
constexpr auto bit_cast (From const &src) noexcept -> To
 Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value representation of the returned To object is equal to the corresponding bit in the object representation of from.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto bit_ceil (UInt x) noexcept -> UInt
 Calculates the smallest integral power of two that is not smaller than x. If that value is not representable in UInt, the behavior is undefined. Call to this function is permitted in constant evaluation only if the undefined behavior does not occur.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto bit_floor (UInt x) noexcept -> UInt
 If x is not zero, calculates the largest integral power of two that is not greater than x. If x is zero, returns zero.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto bit_width (UInt x) noexcept -> int
 If x is not zero, calculates the number of bits needed to store the value x, that is, 1+⌊log2(x)⌋. If x is zero, returns zero.
 
template<integral Int>
constexpr auto byteswap (Int val) noexcept -> Int
 Reverses the bytes in the given integer value n.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto countl_one (UInt x) noexcept -> int
 Returns the number of consecutive 1 ("one") bits in the value of x, starting from the most significant bit ("left").
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto countl_zero (UInt x) noexcept -> int
 Returns the number of consecutive 0 bits in the value of x, starting from the most significant bit ("left").
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto countr_one (UInt x) noexcept -> int
 Returns the number of consecutive 1 bits in the value of x, starting from the least significant bit ("right").
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto countr_zero (UInt x) noexcept -> int
 Returns the number of consecutive 0 bits in the value of x, starting from the least significant bit ("right").
 
template<etl::size_t Pos, etl::builtin_unsigned_integer UInt>
constexpr auto flip_bit (UInt word) noexcept -> UInt
 Flip bit at position Pos.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto flip_bit (UInt word, UInt pos) noexcept -> UInt
 Flip bit at position pos.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto has_single_bit (UInt x) noexcept -> bool
 Checks if x is an integral power of two.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto popcount (UInt val) noexcept -> int
 Returns the number of 1 bits in the value of x.
 
template<etl::size_t Pos, etl::builtin_unsigned_integer UInt>
constexpr auto reset_bit (UInt word) noexcept -> UInt
 Reset bit at position Pos.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto reset_bit (UInt word, UInt pos) noexcept -> UInt
 Reset bit at position pos.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto rotl (UInt t, int s) noexcept -> UInt
 Computes the result of bitwise left-rotating the value of x by s positions. This operation is also known as a left circular shift.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto rotr (UInt t, int s) noexcept -> UInt
 Computes the result of bitwise right-rotating the value of x by s positions. This operation is also known as a right circular shift.
 
template<etl::size_t Pos, etl::builtin_unsigned_integer UInt>
constexpr auto set_bit (UInt word) noexcept -> UInt
 Set bit at position Pos
 
template<etl::size_t Pos, etl::builtin_unsigned_integer UInt>
constexpr auto set_bit (UInt word, bool value) noexcept -> UInt
 Set bit at position Pos to value.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto set_bit (UInt word, UInt pos) noexcept -> UInt
 Set bit at position pos.
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto set_bit (UInt word, UInt pos, bool value) -> UInt
 Set bit at position pos to value.
 
template<etl::size_t Pos, etl::builtin_unsigned_integer UInt>
constexpr auto test_bit (UInt word) noexcept -> bool
 Test bit at position Pos
 
template<etl::builtin_unsigned_integer UInt>
constexpr auto test_bit (UInt word, UInt pos) noexcept -> bool
 Test bit at position pos.
 

Detailed Description

Bit manipulation functions.

#include <etl/bit.hpp>

Enumeration Type Documentation

◆ endian

enum struct endian
strong

Indicates the endianness of all scalar types. If all scalar types are little-endian, endian::native equals endian::little. If all scalar types are big-endian, endian::native equals endian::big.

https://en.cppreference.com/w/cpp/types/endian

Enumerator
little 
big 
native 

Function Documentation

◆ bit_cast()

template<typename To, typename From>
requires detail::bitcastable<To, From>
auto bit_cast ( From const & src) -> To
constexprnoexcept

Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value representation of the returned To object is equal to the corresponding bit in the object representation of from.

The values of padding bits in the returned To object are unspecified. If there is no value of type To corresponding to the value representation produced, the behavior is undefined. If there are multiple such values, which value is produced is unspecified. This overload only participates in overload resolution if sizeof(To) == sizeof(From) and both To and From are TriviallyCopyable types.

https://en.cppreference.com/w/cpp/numeric/bit_cast

◆ bit_ceil()

auto bit_ceil ( UInt x) -> UInt
nodiscardconstexprnoexcept

Calculates the smallest integral power of two that is not smaller than x. If that value is not representable in UInt, the behavior is undefined. Call to this function is permitted in constant evaluation only if the undefined behavior does not occur.

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
The smallest integral power of two that is not smaller than x.

◆ bit_floor()

auto bit_floor ( UInt x) -> UInt
nodiscardconstexprnoexcept

If x is not zero, calculates the largest integral power of two that is not greater than x. If x is zero, returns zero.

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
Zero if x is zero; otherwise, the largest integral power of two that is not greater than x.

◆ bit_width()

auto bit_width ( UInt x) -> int
nodiscardconstexprnoexcept

If x is not zero, calculates the number of bits needed to store the value x, that is, 1+⌊log2(x)⌋. If x is zero, returns zero.

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

◆ byteswap()

template<integral Int>
auto byteswap ( Int val) -> Int
nodiscardconstexprnoexcept

Reverses the bytes in the given integer value n.

etl::byteswap participates in overload resolution only if Int satisfies integral, i.e., Int is an integer type. The program is ill-formed if Int has padding bits.

https://en.cppreference.com/w/cpp/numeric/byteswap

◆ countl_one()

auto countl_one ( UInt x) -> int
nodiscardconstexprnoexcept

Returns the number of consecutive 1 ("one") bits in the value of x, starting from the most significant bit ("left").

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
The number of consecutive 1 bits in the value of x, starting from the most significant bit.

◆ countl_zero()

auto countl_zero ( UInt x) -> int
nodiscardconstexprnoexcept

Returns the number of consecutive 0 bits in the value of x, starting from the most significant bit ("left").

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
The number of consecutive 0 bits in the value of x, starting from the most significant bit.

◆ countr_one()

auto countr_one ( UInt x) -> int
nodiscardconstexprnoexcept

Returns the number of consecutive 1 bits in the value of x, starting from the least significant bit ("right").

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
The number of consecutive 1 bits in the value of x, starting from the least significant bit.

◆ countr_zero()

auto countr_zero ( UInt x) -> int
nodiscardconstexprnoexcept

Returns the number of consecutive 0 bits in the value of x, starting from the least significant bit ("right").

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
The number of consecutive 0 bits in the value of x, starting from the least significant bit.

◆ flip_bit() [1/2]

auto flip_bit ( UInt word) -> UInt
nodiscardconstexprnoexcept

Flip bit at position Pos.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Note
Non-standard extension

◆ flip_bit() [2/2]

auto flip_bit ( UInt word,
UInt pos ) -> UInt
nodiscardconstexprnoexcept

Flip bit at position pos.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Precondition
Position pos must be a valid bit-index for UInt
Note
Non-standard extension

◆ has_single_bit()

auto has_single_bit ( UInt x) -> bool
nodiscardconstexprnoexcept

Checks if x is an integral power of two.

This overload only participates in overload resolution if T is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Returns
true if x is an integral power of two; otherwise false.

◆ popcount()

auto popcount ( UInt val) -> int
nodiscardconstexprnoexcept

Returns the number of 1 bits in the value of x.

This overload only participates in overload resolution if UInt is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

◆ reset_bit() [1/2]

auto reset_bit ( UInt word) -> UInt
nodiscardconstexprnoexcept

Reset bit at position Pos.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Note
Non-standard extension

◆ reset_bit() [2/2]

auto reset_bit ( UInt word,
UInt pos ) -> UInt
nodiscardconstexprnoexcept

Reset bit at position pos.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Precondition
Position pos must be a valid bit-index for UInt
Note
Non-standard extension

◆ rotl()

auto rotl ( UInt t,
int s ) -> UInt
constexprnoexcept

Computes the result of bitwise left-rotating the value of x by s positions. This operation is also known as a left circular shift.

◆ rotr()

auto rotr ( UInt t,
int s ) -> UInt
constexprnoexcept

Computes the result of bitwise right-rotating the value of x by s positions. This operation is also known as a right circular shift.

◆ set_bit() [1/4]

auto set_bit ( UInt word) -> UInt
nodiscardconstexprnoexcept

◆ set_bit() [2/4]

auto set_bit ( UInt word,
bool value ) -> UInt
nodiscardconstexprnoexcept

Set bit at position Pos to value.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Note
Non-standard extension

◆ set_bit() [3/4]

auto set_bit ( UInt word,
UInt pos ) -> UInt
nodiscardconstexprnoexcept

Set bit at position pos.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Precondition
Position pos must be a valid bit-index for UInt
Note
Non-standard extension

◆ set_bit() [4/4]

auto set_bit ( UInt word,
UInt pos,
bool value ) -> UInt
nodiscardconstexpr

Set bit at position pos to value.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Precondition
Position pos must be a valid bit-index for UInt
Note
Non-standard extension

◆ test_bit() [1/2]

auto test_bit ( UInt word) -> bool
nodiscardconstexprnoexcept

◆ test_bit() [2/2]

auto test_bit ( UInt word,
UInt pos ) -> bool
nodiscardconstexprnoexcept

Test bit at position pos.

https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit

Precondition
Position pos must be a valid bit-index for UInt
Note
Non-standard extension