BITSTRING(3)
π BITSTRING(3) β bit-string manipulation macros, bestie edition π
okay so this is bitstring.h, the library that gives you the bit-string manipulation macros, and honestly? iconic. the whole friend group is here: bit_alloc, bit_clear, bit_decl, bit_ffs, bit_nclear, bit_nset, bit_set, bitstr_size, bit_test. all of them. no gatekeeping.
β¨ SYNOPSIS β¨
like, literally just include the header and you're in:
#include <bitstring.h>
then the whole cast, in order:
name = bit_alloc(nbits)
bitstr_t *name;
int nbits;
bit_decl(name, nbits)
bitstr_t name;
int nbits;
bit_clear(name, bit)
bitstr_t name;
int bit;
bit_ffc(name, nbits, value)
bitstr_t name;
int nbits, *value;
bit_ffs(name, nbits, value)
bitstr_t name;
int nbits, *value;
bit_nclear(name, start, stop)
bitstr_t name;
int start, stop;
bit_nset(name, start, stop)
bitstr_t name;
int start, stop;
bit_set(name, bit)
bitstr_t name;
int bit;
bitstr_size(nbits)
int nbits;
bit_test(name, bit)
bitstr_t name;
int bit;
π¦ DESCRIPTION π¦
so these macros operate on strings of bits. that's the whole vibe. let's go one by one π
bit_allocβ returns a pointer of typebitstr_t *to enough space to storenbitsbits. slay. but if no space is available it hands youNULLinstead, which is the group-chat equivalent of being left on read πbit_declβ this is the macro that allocates enough space to storenbitsbits on the stack. stack girlie behavior, we respect it.bitstr_sizeβ returns the number of elements of typebitstr_tthat you need to storenbitsbits. super useful for copying bit strings, so keep it in your back pocket fr fr.bit_clearandbit_setβ clear or set the zero-based numbered bitbit, in the bit stringname. zero-based!! don't come at me with your 1-indexed nonsense bestie.bit_nsetandbit_nclearβ set or clear the zero-based numbered bits fromstarttostop, in the bit stringname. it's giving bulk edit energy π₯bit_testβ evaluates to zero if the zero-based numbered bitbitof bit stringnameis set, and non-zero otherwise. yes, that's backwards from what your brain wants. yes, it's still correct. no cap.bit_ffsβ sets*valueto the zero-based number of the first bit set in the array ofnbitsbits referenced byname. and if no bits are set,*valuegets set to-1. iykyk.bit_ffcβ sets*valueto the zero-based number of the first bit not set in the array ofnbitsbits referenced byname. if all the bits are set,valueis set to-1. (yep, the manpage saysvaluehere, not*valueβ we're keeping it exactly as written π)
πΈ EXAMPLE πΈ
here's a whole lil printer-availability situation, verbatim, because i would never do you dirty by paraphrasing code:
#include <limits.h>
#include <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);
}
}
π SEE ALSO π
malloc(3)β where the actual memory magic comes from, king behavior π
π heads up: bit_test returning zero when the bit is set is the kind of thing that will ruin your whole afternoon if you forget it. write it on your mirror. zero means set. periodt.
π«Ά okay that's the whole page, bestie β this one's from the 4th Berkeley Distribution, dated December 13, 1989, and it's been serving BITSTRING(3) realness ever since. mwah πβ¨
π¦ where this page comes from
- alpine 3.24: fcron-doc 3.4.0-r1 Β· GPL-2.0-or-later
- nixos 26.05: fcron 3.4.0
- nixpkgs unstable: fcron 3.4.1
- Copyright (c) 1989 The Regents of the University of California
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms