bitstring(3bsd)
β¨ 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, orNULLif no space is available. so if you getNULL, 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 typebitstr_tnecessary 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()andbit_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()andbit_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
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian bookworm: libbsd-dev 0.11.7-2
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- nixpkgs unstable: libbsd 0.12.2
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms