Sturk 1.0.2
Publish-subscribe C implementation.
Loading...
Searching...
No Matches
bits.h File Reference

Bits and bitfields. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define st_bf_set(word, mask, val)   (((word) & ~(mask)) | (val))
 Set a bitfield in a word to a new value.
 
#define ST_BIT(pos)   (1 << (pos))
 Create a bitmask for a single bit.
 
#define ST_BITMASK(hi, lo)   ((1 << (hi)) * 2 - (1 << (lo)))
 Create a contiguous bitmask.
 
#define ST_BITFIELD(mask, val)   ((val) << (__builtin_ffs(mask) - 1))
 Create a bitfield.
 

Typedefs

typedef int StBits
 The data type for bitwise operations.
 

Detailed Description

Bits and bitfields.

This header file provides macros for operating with bits and bitfields. Use the StBits data type for bitwise operations as it avoids many issues related to integral promotion, which is a common source of bugs when doing bit manipulations.

Note
Compiling with -Wconversion is recommended.

Usage.

  1. Set.
    word |= ST_BIT(pos);
    word |= ST_BITMASK(hi, lo);
    #define ST_BITMASK(hi, lo)
    Create a contiguous bitmask.
    Definition bits.h:95
    #define ST_BIT(pos)
    Create a bitmask for a single bit.
    Definition bits.h:83
  2. Clear.
    word &= ~ST_BIT(pos);
    word $= ~ST_BITMASK(hi, lo);

Macro Definition Documentation

◆ st_bf_set

#define st_bf_set (   word,
  mask,
  val 
)    (((word) & ~(mask)) | (val))

Set a bitfield in a word to a new value.

Parameters
[in]wordThe word that contains the bitfield.
[in]maskThe bitmask of the bitfield.
[in]valThe new value of the bitfield.
Returns
New value of the word.

◆ ST_BIT

#define ST_BIT (   pos)    (1 << (pos))

Create a bitmask for a single bit.

This macro creates a bitmask for a bit at position pos.

Returns
A bitmask for the bit at the pos position.

◆ ST_BITFIELD

#define ST_BITFIELD (   mask,
  val 
)    ((val) << (__builtin_ffs(mask) - 1))

Create a bitfield.

Parameters
[in]maskThe bitmask of the bitfield.
[in]valThe value of the bitfield.
Returns
A bitfield with the value val at the position of the mask.

◆ ST_BITMASK

#define ST_BITMASK (   hi,
  lo 
)    ((1 << (hi)) * 2 - (1 << (lo)))

Create a contiguous bitmask.

This macro creates a contiguous bitmask that spans from the hi (most significant bit) to the lo (least significant bit).

Returns
A contiguous bitmask that spans from the hi to the lo.