Skip to content

Commit

Permalink
sys/bitfield: add support for bitfield operations
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Feb 26, 2022
1 parent 039e993 commit 68cfa2c
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions sys/include/bitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,65 @@ static inline bool bf_isset(uint8_t field[], size_t idx)
return (field[idx / 8] & (1u << (7 - (idx % 8))));
}

/**
* @brief Perform a bitwise OR operation on two bitfields
* `out = a | b`
*
* @param[out] out The resulting bitfield
* @param[in] a The first bitfield
* @param[in] b The second bitfield
*/
static inline void bf_or(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
{
while (len--) {
out[len] = a[len] | b[len];
}
}

/**
* @brief Perform a bitwise AND operation on two bitfields
* `out = a & b`
*
* @param[out] out The resulting bitfield
* @param[in] a The first bitfield
* @param[in] b The second bitfield
*/
static inline void bf_and(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
{
while (len--) {
out[len] = a[len] & b[len];
}
}

/**
* @brief Perform a bitwise XOR operation on two bitfields
* `out = a ^ b`
*
* @param[out] out The resulting bitfield
* @param[in] a The first bitfield
* @param[in] b The second bitfield
*/
static inline void bf_xor(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
{
while (len--) {
out[len] = a[len] ^ b[len];
}
}

/**
* @brief Perform a bitwise NOT operation on a bitfield
* `out = ~a`
*
* @param[out] out The resulting bitfield
* @param[in] a The bitfield to invert
*/
static inline void bf_inv(uint8_t out[], const uint8_t a[], size_t len)
{
while (len--) {
out[len] = ~a[len];
}
}

/**
* @brief Atomically get the number of an unset bit and set it
*
Expand Down

0 comments on commit 68cfa2c

Please sign in to comment.