bitfield.h

bitfields operations on bitfields of arbitrary length

Note

Code taken mostly from Stackoverflow, User Christoph

BITFIELD( NAME, SIZE)

Declare a bitfield of a given size.

1
uint8_t NAME[((SIZE) + 7) / 8]

Note

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

void bf_set(uint8_t field, msp430_types.h::size_t idx)

Set the bit to 1.

Parameters

field:The bitfield
idx:The number of the bit to set

void bf_unset(uint8_t field, msp430_types.h::size_t idx)

Clear the bit.

Parameters

field:The bitfield
idx:The number of the bit to clear

void bf_toggle(uint8_t field, msp430_types.h::size_t idx)

Toggle the bit.

Parameters

field:The bitfield
idx:The number of the bit to toggle

bool bf_isset(uint8_t field, msp430_types.h::size_t idx)

Check if the bet is set.

Parameters

field:The bitfield
idx:The number of the bit to check

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

field:The bitfield
size:The size of the bitfield

Return values

  • number of bit that was set
  • -1 if no bit was unset