bitstring(3bsd)

πŸ“¦
version 0.12.2 Β· 2 versions
  • 0.12.2 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.11.5 Β· ubuntu jammy
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ bitstring β€” the bit-string manipulation macros bestie era ✨

okay so this is bitstring(3bsd) from the LOCAL manual, and it's literally just a bunch of macros for messing with strings of bits. tiny bits. baby bits. let's get into it πŸ’…

🫢 NAME

bit_alloc, bit_clear, bit_decl, bit_ffs, bit_nclear, bit_nset, bit_set, bitstr_size, bit_test β€” bit-string manipulation macros

πŸ“š LIBRARY

Utility functions from BSD systems (libbsd, -lbsd)

🌸 SYNOPSIS

#include <bitstring.h>
(See libbsd(7) for include usage.)

bitstr_t *
bit_alloc(int nbits);

void
bit_decl(bitstr_t *name, int nbits);

void
bit_clear(bitstr_t *name, int bit);

void
bit_ffc(bitstr_t *name, int nbits, int *value);

void
bit_ffs(bitstr_t *name, int nbits, int *value);

void
bit_nclear(bitstr_t *name, int start, int stop);

void
bit_nset(bitstr_t *name, int start, int stop);

void
bit_set(bitstr_t *name, int bit);

int
bitstr_size(int nbits);

int
bit_test(bitstr_t *name, int bit);
also bestie: the include usage deets live in libbsd(7), go peek there if you need them πŸ’–

πŸ‘‘ DESCRIPTION

These macros operate on strings of bits. yes, strings. of bits. it's giving very low-level, very cunty, very efficient ✨

  • The macro bit_alloc() returns a pointer of type β€œbitstr_t *” to sufficient space to store nbits bits, or NULL if no space is available. so if you get NULL, that's your sign, no room at the inn 😭
  • The macro bit_decl() allocates sufficient space to store nbits bits on the stack. stack queen behavior πŸŽ€
  • The macro bitstr_size() returns the number of elements of type bitstr_t necessary to store nbits bits. This is useful for copying bit strings. lowkey the copy-paste bestie of this whole friend group πŸ¦‹
  • The macros bit_clear() and bit_set() clear or set the zero-based numbered bit bit, in the bit string name. zero-based, bestie, not one-based, do not come for me πŸ™ˆ
  • The bit_nset() and bit_nclear() macros set or clear the zero-based numbered bits from start through stop in the bit string name. it's a range, it goes from start all the way through stop, inclusive energy πŸ’œ
  • The bit_test() macro evaluates to non-zero if the zero-based numbered bit bit of bit string name is set, and zero otherwise. so like… is it on or is it off, no in-between ⭐
  • The bit_ffs() macro stores in the location referenced by value the zero-based number of the first bit set in the array of nbits bits referenced by name. If no bits are set, the location referenced by value is set to -1. ffs = first first set, iconic abbreviation, ate πŸ”₯
  • The macro bit_ffc() stores in the location referenced by value the zero-based number of the first bit not set in the array of nbits bits referenced by name. If all bits are set, the location referenced by value is set to -1. that's first first clear, for the record πŸ’…
  • The arguments to these macros are evaluated only once and may safely have side effects. this is the good news bestie, no double-evaluation jumpscare, we're safe 🫢

🌈 EXAMPLES

here's the whole vibe, verbatim, don't touch a character:

#include <limits.h>
#include <bsd/bitstring.h>

...
#define LPR_BUSY_BIT            0
#define LPR_FORMAT_BIT          1
#define LPR_DOWNLOAD_BIT        2
...
#define LPR_AVAILABLE_BIT       9
#define LPR_MAX_BITS            10

make_lpr_available()
{
        bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
        ...
        bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
        ...
        if (!bit_test(bitlist, LPR_BUSY_BIT)) {
                bit_clear(bitlist, LPR_FORMAT_BIT);
                bit_clear(bitlist, LPR_DOWNLOAD_BIT);
                bit_set(bitlist, LPR_AVAILABLE_BIT);
        }
}

like… the way she declares the bits on the stack, nukes the whole range with bit_nclear, then checks if she's busy before setting herself available?? main character energy fr fr πŸ’–


πŸ” SEE ALSO


πŸ•°οΈ HISTORY

The bitstring functions first appeared in 4.4BSD. old money. vintage. still slaying to this day πŸ‘‘


and that's the whole page bestie!! this one shipped from Debian and it's dated July 19, 1993 β€” yes, 1993, she's been serving bits since before most of us existed πŸ˜­πŸ’€ go forth and manipulate those bit strings, i'm obsessed with you, mwah πŸ’‹πŸŽ€

πŸ“¦ where this page comes from