BITS(3)
β¨ BITS(3) β¨
okay bestie, welcome to the Library Functions Manual page for the bits macros. this is the group chat where bitmasks get cute and bitfields get handled. no cap, it's giving useful. π
πΈ NAME πΈ
__BIT, __BITS, __MASK, __SHIFTIN, __SHIFTOUT, __SHIFTOUT_MASK β macros for preparing bitmasks and operating on bit fields
π SYNOPSIS π
#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);
π¦ DESCRIPTION π¦
okay so these macros are literally the main characters when you need to 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. fr fr. π«Ά
Use __BIT(), __BITS(), and __MASK() to define bitmasks:
__BIT(n)β gives you back a bitmask with bitnset, and the least significant bit is bit0. π__BITS(m, n)β gives you back a bitmask with bitsmthroughn, inclusive, set. no stress about whetherm > norm <= n, it does not matter. the least significant bit is bit0. β¨__MASK(n)β gives you back a bitmask with the firstnbits set, aka bits0throughn - 1, inclusive. π
__SHIFTIN(), __SHIFTOUT(), and __SHIFTOUT_MASK() help read and write bitfields from words:
__SHIFTIN(v, mask)β left-shifts bitsvinto the bitfield defined bymask, then returns them. no side-effects. it's giving clean. π__SHIFTOUT(v, mask)β pulls out and returns the bitfield selected bymaskfromv, right-shifting the bits so the rightmost selected bit is at bit0. no side-effects. π__SHIFTOUT_MASK(mask)β right-shifts the bits inmaskso the rightmost non-zero bit is at bit0. 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). π
π EXAMPLES π
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 */
π SEE ALSO π
π HISTORY π
the bits macros first appeared in atw(4), with different names and implementation. in their current form these macros appeared in NetBSD 4.0. lowkey iconic. π§
π AUTHORS π
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(). π
okay that's the whole page, bestie π«Ά β source/version: Debian, date: January 22, 2022, manual: BITS(3). slay. β¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2006, 2010 David Young. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms