Embedded Artistry Framework
Embedded Systems C++ Framework
Classes
Bit Manipulation

Functions and macros that operate in a bitwise manner. More...

Collaboration diagram for Bit Manipulation:

Classes

struct  embutil::countbits< tval >
 Template struct for counting set bits and significant bits at compile-time. More...
 

Alignment Macros

#define align_up(num, align)   (((num) + ((align)-1)) & ~((align)-1))
 Increase a number to the next aligned value. More...
 
#define IS_POWER_2(x)   (!((x) & ((x)-1)))
 Check if a number is a power of 2. More...
 
#define IS_ALIGNED(val, align)   ((val & (align - 1)) == 0)
 Check if a value is aligned. More...
 

Bitfield Macros

#define SET_BIT(x)   (1 << x)
 Set bit at position X. More...
 
#define EXTRACT_BITFIELD(value, high_bit, low_bit)   (((value) >> (low_bit)) & N_SET_BITS((high_bit) - (low_bit) + 1))
 Extract a field of bits from a value. More...
 

Count Bits Macros

#define N_SET_BITS(n_ones)   ((1 << (n_ones)) - 1)
 Count the number of set bits. More...
 

Alignment Functions

template<typename TIntegralType >
constexpr bool embutil::is_power_2 (TIntegralType val) noexcept
 Check if a number is a power of 2. More...
 
template<typename TType >
constexpr bool embutil::is_aligned (const TType val, const size_t align) noexcept
 Checks if a value meets a specified alignment. More...
 

Bitfield Functions

template<typename TIntegralType , typename... Args>
constexpr TIntegralType embutil::bitmask (Args &&... args) noexcept
 A C++ 17 bitmask generator. More...
 

Detailed Description

Functions and macros that operate in a bitwise manner.


Class Documentation

◆ embutil::countbits

struct embutil::countbits

template<unsigned tval>
struct embutil::countbits< tval >

Template struct for counting set bits and significant bits at compile-time.

Examples:

countbits<8>::setbits == 1 (0b1000)
countbits<9>::setbits == 2 (0b1100)
countbits<8>::significant == 4 (0b1111)
countbits<9>::significant == 4 (0b1111)
Template Parameters
tvalUnsigned integral value to analyze.

Public Types

using t_ = countbits< tval/2 >
 

Static Public Attributes

static constexpr unsigned set = ((tval & 1) != 0) ? t_::set + 1 : t_::set
 Shows how many bits in the value are set. More...
 
static constexpr unsigned significant_bits = (tval != 0) ? t_::significant_bits + 1 : 0
 shows how many bits in the value matter More...
 

Member Typedef Documentation

◆ t_

template<unsigned tval>
using embutil::countbits< tval >::t_ = countbits<tval / 2>

Member Data Documentation

◆ set

template<unsigned tval>
constexpr unsigned embutil::countbits< tval >::set = ((tval & 1) != 0) ? t_::set + 1 : t_::set
static

Shows how many bits in the value are set.

◆ significant_bits

template<unsigned tval>
constexpr unsigned embutil::countbits< tval >::significant_bits = (tval != 0) ? t_::significant_bits + 1 : 0
static

shows how many bits in the value matter

Macro Definition Documentation

◆ align_up

#define align_up (   num,
  align 
)    (((num) + ((align)-1)) & ~((align)-1))

Increase a number to the next aligned value.

Parameters
numThe number to start with.
alignThe target alignment.
Returns
The next value greater than num matching the target alignment.

◆ EXTRACT_BITFIELD

#define EXTRACT_BITFIELD (   value,
  high_bit,
  low_bit 
)    (((value) >> (low_bit)) & N_SET_BITS((high_bit) - (low_bit) + 1))

Extract a field of bits from a value.

Given a number, such as: 0b11010100

If we wanted to extract bits 7..5, we would supply: high_bit = 7 low_bit = 5

And the result would be 0b110

Parameters
valueThe value to extract the bitfield from.
high_bitthe bit representing the upper limit of the bitfield.
low_bitthe bit representing the lower limit of the bitfield.
Returns
the extracted bitfield, shifted right by low_bit.

◆ IS_ALIGNED

#define IS_ALIGNED (   val,
  align 
)    ((val & (align - 1)) == 0)

Check if a value is aligned.

Parameters
valThe number to check.
alignThe target alignment.
Returns
true if the number matches the target alignment, false otherwise.

◆ IS_POWER_2

#define IS_POWER_2 (   x)    (!((x) & ((x)-1)))

Check if a number is a power of 2.

Parameters
xThe number to check.
Returns
true if the number is a power of 2, false otherwise.

◆ N_SET_BITS

#define N_SET_BITS (   n_ones)    ((1 << (n_ones)) - 1)

Count the number of set bits.

Parameters
n_onesThe number to check.
Returns
the number of set bits.

◆ SET_BIT

#define SET_BIT (   x)    (1 << x)

Set bit at position X.

Parameters
xThe bit position to set.
Returns
A number with bit x set.

Function Documentation

◆ bitmask()

template<typename TIntegralType , typename... Args>
constexpr TIntegralType embutil::bitmask ( Args &&...  args)
noexcept

A C++ 17 bitmask generator.

This function was inspired by Martin Moene.

Example: bitmask<uint8_t>(6, 5, 0) -> 0b0110'0001

Template Parameters
TIntegralTypeThe desired type for the bitmask.
ArgsA variadic list corresponding to bits to be set. Args is detected by the compiler.
Parameters
argsThe variadic list of bits to set in the bitmask.
Returns
A number of type TIntegralType with the bits in the input list set.

◆ is_aligned()

template<typename TType >
constexpr bool embutil::is_aligned ( const TType  val,
const size_t  align 
)
inlinenoexcept

Checks if a value meets a specified alignment.

Behavior for integral types: check if the value is aligned.

Behavior for pointers: check if the pointer is aligned.

Behavior for non-integral non-pointer types: Take the address and determine if it is aligned.

Template Parameters
TTypethe type of data to check the alignment of. TType can be integral, pointer, or non-integral and non-pointer. The behavior of the function changes depending on the type.
Parameters
valThe value to check the alignment of.
alignThe target alignment.
Returns
true if the value has the target alignment, false otherwise.

References IS_ALIGNED, and r.

Referenced by os::posix::Thread::Thread().

◆ is_power_2()

template<typename TIntegralType >
constexpr bool embutil::is_power_2 ( TIntegralType  val)
noexcept

Check if a number is a power of 2.

Template Parameters
TIntegralTypeThe type of the number to check. This type does not need to be manually specified. The compiler should automatically deduce this type. TIntegralType must be an integral type.
Parameters
valThe number to evaluate.
Returns
true if the number is a power of 2, false otherwise.

References IS_POWER_2.