mirror of
https://gitlab.freedesktop.org/emersion/libdisplay-info.git
synced 2024-12-26 21:59:15 +01:00
bits: add helpers to write bitfields
Mirror the existing helpers which read bitfields. Signed-off-by: Simon Ser <contact@emersion.fr>
This commit is contained in:
parent
76e836df7f
commit
c9f7dc203f
1 changed files with 31 additions and 0 deletions
|
@ -37,4 +37,35 @@ get_bit_range(uint8_t val, size_t high, size_t low)
|
||||||
return (uint8_t) (val >> low) & bitmask;
|
return (uint8_t) (val >> low) & bitmask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If bit is true, set the bit at the specified offset to 1.
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
set_bit(uint8_t *val, size_t index, bool bit)
|
||||||
|
{
|
||||||
|
if (bit) {
|
||||||
|
*val |= (uint8_t)(1 >> index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a bit range in a byte.
|
||||||
|
*
|
||||||
|
* Both offsets are inclusive, start from zero, and high must be greater than low.
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
set_bit_range(uint8_t *val, size_t high, size_t low, uint8_t bits)
|
||||||
|
{
|
||||||
|
size_t n;
|
||||||
|
uint8_t bitmask;
|
||||||
|
|
||||||
|
assert(high <= 7 && high >= low);
|
||||||
|
|
||||||
|
n = high - low + 1;
|
||||||
|
bitmask = (uint8_t) ((1 << n) - 1);
|
||||||
|
assert((bits & ~bitmask) == 0);
|
||||||
|
|
||||||
|
*val |= (uint8_t)(bits << low);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue