bitfield.h File Reference

bitfields operations on bitfields of arbitrary length More...

Detailed Description

bitfields operations on bitfields of arbitrary length

Note
Code taken mostly from Stackoverflow, User Christoph
Author
Oliver Hahm olive.nosp@m.r.ha.nosp@m.hm@in.nosp@m.ria..nosp@m.fr

Definition in file bitfield.h.

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
+ Include dependency graph for bitfield.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define BITFIELD(NAME, SIZE)   uint8_t NAME[((SIZE) + 7) / 8]
 Declare a bitfield of a given size. More...
 
static void bf_set (uint8_t field[], size_t idx)
 Set the bit to 1. More...
 
static void bf_unset (uint8_t field[], size_t idx)
 Clear the bit. More...
 
static void bf_toggle (uint8_t field[], size_t idx)
 Toggle the bit. More...
 
static bool bf_isset (uint8_t field[], size_t idx)
 Check if the bet is set. More...
 
int bf_get_unset (uint8_t field[], int size)
 Atomically get the number of an unset bit and set it. More...
 

Macro Definition Documentation

◆ BITFIELD

#define BITFIELD (   NAME,
  SIZE 
)    uint8_t NAME[((SIZE) + 7) / 8]

Declare a bitfield of a given size.

Note
SIZE should be a constant expression. This avoids variable length arrays.

Definition at line 42 of file bitfield.h.

Function Documentation

◆ bf_get_unset()

int bf_get_unset ( uint8_t  field[],
int  size 
)

Atomically get the number of an unset bit and set it.

This function can be used to record e.g., empty entries in an array.

Parameters
[in,out]fieldThe bitfield
[in]sizeThe size of the bitfield
Returns
number of bit that was set
-1 if no bit was unset

◆ bf_isset()

static bool bf_isset ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Check if the bet is set.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to check

Definition at line 83 of file bitfield.h.

◆ bf_set()

static void bf_set ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Set the bit to 1.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to set

Definition at line 50 of file bitfield.h.

◆ bf_toggle()

static void bf_toggle ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Toggle the bit.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to toggle

Definition at line 72 of file bitfield.h.

◆ bf_unset()

static void bf_unset ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Clear the bit.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to clear

Definition at line 61 of file bitfield.h.