These macros prepare bitmasks, extract bitfields from words, and insert bitfields into words. A “bitfield” is a span of consecutive bits defined by a bitmask, where 1s select the bits in the bitfield.
Use
__BIT() and
__BITS() to define bitmasks:
__BIT(n)
Return a bitmask with bit m set, where the least significant bit is bit 0.
__BITS(m, n)
Return a bitmask with bits m through n, inclusive, set. It does not matter whether m > n or m <= n. The least significant bit is bit 0.
__SHIFTIN(),
__SHIFTOUT(), and
__SHIFTOUT_MASK() help read and write bitfields from words:
__SHIFTIN(v, mask)
Left-shift bits v into the bitfield defined by mask, and return them. No side-effects.
__SHIFTOUT(v, mask)
Extract and return the bitfield selected by mask from v, right-shifting the bits so that the rightmost selected bit is at bit 0. No side-effects.
__SHIFTOUT_MASK(mask)
Right-shift the bits in mask so that the rightmost non-zero bit is at bit 0. This is useful for finding the greatest unsigned value that a bitfield can hold. No side-effects. Note that __SHIFTOUT_MASK(m) = __SHIFTOUT(m, m).