| BITS(3) | Library Functions Manual | BITS(3) |
__BIT, __BITS,
__MASK, __SHIFTIN,
__SHIFTOUT, __SHIFTOUT_MASK
— macros for preparing bitmasks and operating on bit
fields
#include
<sys/param.h>
#include <sys/cdefs.h>
uintmax_t
__BIT(n);
uintmax_t
__BITS(m,
n);
uintmax_t
__MASK(n);
uintmax_t
__SHIFTIN(v,
mask);
uintmax_t
__SHIFTOUT(v,
mask);
uintmax_t
__SHIFTOUT_MASK(mask);
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(),
__BITS(),
and
__MASK()
to define bitmasks:
__BIT(n)__BITS(m,
n)__MASK(n)__SHIFTIN(),
__SHIFTOUT(),
and
__SHIFTOUT_MASK()
help read and write bitfields from words:
__SHIFTIN(v,
mask)__SHIFTOUT(v,
mask)__SHIFTOUT_MASK(mask)__SHIFTOUT_MASK(m) =
__SHIFTOUT(m,
m).The following example demonstrates basic usage of the
bits macros:
uint32_t bits, mask, val; bits = __BITS(2, 3); /* 00001100 */ mask = __BIT(2) | __BIT(3); /* 00001100 */ val = __SHIFTIN(0x03, mask); /* 00001100 */ val = __SHIFTOUT(0xf, mask); /* 00000011 */
The bits macros first appeared in
atw(4), with different names and
implementation. In their current form these macros appeared in
NetBSD 4.0.
The bits macros were written by
David Young
<dyoung@NetBSD.org>.
Matt Thomas
<matt@NetBSD.org>
suggested important improvements to the implementation, and contributed the
macro names SHIFTIN() and
SHIFTOUT().
| January 22, 2022 | NetBSD 11.0 |