BITSET(9)
β¨ NAME β¨
okay so bestie, meet bitset(9) π β it's not one thing, it's a whole squad. This is the bitset manipulation macros family, and yes it's a long list, and yes I'm giving every single one of them their moment:
BITSET_DEFINE, BITSET_T_INITIALIZER, BITSET_FSET, BIT_CLR, BIT_COPY, BIT_ISSET, BIT_SET, BIT_ZERO, BIT_FILL, BIT_SETOF, BIT_EMPTY, BIT_ISFULLSET, BIT_FFS, BIT_FLS, BIT_COUNT, BIT_SUBSET, BIT_OVERLAP, BIT_CMP, BIT_OR, BIT_OR2, BIT_AND, BIT_AND2, BIT_NAND, BIT_NAND2, BIT_XOR, BIT_XOR2, BIT_CLR_ATOMIC, BIT_SET_ATOMIC, BIT_SET_ATOMIC_ACQ, BIT_AND_ATOMIC, BIT_OR_ATOMIC, BIT_COPY_STORE_REL.
That's the roster. Kernel Developer's Manual energy, but make it cute π
π¦ SYNOPSIS π¦
#include <sys/_bitset.h>
#include <sys/bitset.h>
BITSET_DEFINE(STRUCTNAME, const SETSIZE);
BITSET_T_INITIALIZER(ARRAY_CONTENTS);
BITSET_FSET(N_WORDS);
BIT_CLR(const SETSIZE, size_t bit, struct STRUCTNAME *bitset);
BIT_COPY(const SETSIZE, struct STRUCTNAME *from, struct STRUCTNAME *to);
bool
BIT_ISSET(const SETSIZE, size_t bit, struct STRUCTNAME *bitset);
BIT_SET(const SETSIZE, size_t bit, struct STRUCTNAME *bitset);
BIT_ZERO(const SETSIZE, struct STRUCTNAME *bitset);
BIT_FILL(const SETSIZE, struct STRUCTNAME *bitset);
BIT_SETOF(const SETSIZE, size_t bit, struct STRUCTNAME *bitset);
bool
BIT_EMPTY(const SETSIZE, struct STRUCTNAME *bitset);
bool
BIT_ISFULLSET(const SETSIZE, struct STRUCTNAME *bitset);
int
BIT_FFS(const SETSIZE, struct STRUCTNAME *bitset);
int
BIT_FLS(const SETSIZE, struct STRUCTNAME *bitset);
int
BIT_COUNT(const SETSIZE, struct STRUCTNAME *bitset);
bool
BIT_SUBSET(const SETSIZE, struct STRUCTNAME *haystack,
struct STRUCTNAME *needle);
bool
BIT_OVERLAP(const SETSIZE, struct STRUCTNAME *bitset1,
struct STRUCTNAME *bitset2);
bool
BIT_CMP(const SETSIZE, struct STRUCTNAME *bitset1,
struct STRUCTNAME *bitset2);
BIT_OR(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src);
BIT_OR2(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src1,
struct STRUCTNAME *src2);
BIT_AND(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src);
BIT_AND2(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src1,
struct STRUCTNAME *src2);
BIT_NAND(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src);
BIT_NAND2(const SETSIZE, struct STRUCTNAME *dst,
struct STRUCTNAME *src1, struct STRUCTNAME *src2);
BIT_XOR(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src);
BIT_XOR2(const SETSIZE, struct STRUCTNAME *dst, struct STRUCTNAME *src1,
struct STRUCTNAME *src2);
BIT_CLR_ATOMIC(const SETSIZE, size_t bit, struct STRUCTNAME *bitset);
BIT_SET_ATOMIC(const SETSIZE, size_t bit, struct STRUCTNAME *bitset);
BIT_SET_ATOMIC_ACQ(const SETSIZE, size_t bit,
struct STRUCTNAME *bitset);
BIT_AND_ATOMIC(const SETSIZE, struct STRUCTNAME *dst,
struct STRUCTNAME *src);
BIT_OR_ATOMIC(const SETSIZE, struct STRUCTNAME *dst,
struct STRUCTNAME *src);
BIT_COPY_STORE_REL(const SETSIZE, struct STRUCTNAME *from,
struct STRUCTNAME *to);
π DESCRIPTION π
The bitset(9) family of macros hands you a flexible and efficient bitset implementation β but only if the maximum size of the set is known at compilation. That's the deal, that's the vibe check, no cap.
Throughout this whole page, the name SETSIZE means the size of the bitset in bits. Individual bits get referenced with indices zero through SETSIZE - 1. And if you want a real-life example of <sys/bitset.h> out in the wild, look at <sys/cpuset.h> π¦
BITSET_DEFINE()β defines a bitsetstruct STRUCTNAMEwith room to representSETSIZEbits.BITSET_T_INITIALIZER()β lets you initialize a bitset struct with a compile time literal value.BITSET_FSET()β generates a compile time literal, usable byBITSET_T_INITIALIZER(), representing a full bitset (all bits set). For examples ofBITSET_T_INITIALIZER()andBITSET_FSET()usage, see the βBITSET_T_INITIALIZER EXAMPLEβ section. TheN_WORDSparameter toBITSET_FSET()should be:
__bitset_words(SETSIZE)
BIT_CLR()β clears bit bit in the bitset pointed to by bitset.BIT_CLR_ATOMIC()is identical, but the bit is cleared atomically.BIT_COPY()β copies the contents of the bitset from to the bitset to.BIT_COPY_STORE_REL()is similar, but copies component machine words from from and writes them to to with atomic store with release semantics. (That is, if to is composed of multiple machine words,BIT_COPY_STORE_REL()performs multiple individually atomic operations.) π«ΆBIT_SET()β sets bit bit in the bitset pointed to by bitset.BIT_SET_ATOMIC()is identical, but the bit is set atomically.BIT_SET_ATOMIC_ACQ()sets the bit with acquire semantics.BIT_ZERO()β clears all bits in bitset.BIT_FILL()β sets all bits in bitset.BIT_SETOF()β clears all bits in bitset before setting only bit bit. It's giving "scorched earth, then one single star" βBIT_EMPTY()β returnstrueif bitset is empty.BIT_ISFULLSET()β returnstrueif bitset is full (all bits set).BIT_FFS()β returns the 1-index of the first (lowest) set bit in bitset, or zero if bitset is empty. Like withffs(3), to use the non-zero result ofBIT_FFS()as a bit index parameter to any otherbitset(9)macro, you must subtract one from the result. πBIT_FLS()β returns the 1-index of the last (highest) set bit in bitset, or zero if bitset is empty. Like withfls(3), to use the non-zero result ofBIT_FLS()as a bit index parameter to any otherbitset(9)macro, you must subtract one from the result. Same energy, opposite end, iykyk.BIT_COUNT()β returns the total number of set bits in bitset.BIT_SUBSET()β returnstrueif needle is a subset of haystack. πͺ‘BIT_OVERLAP()β returnstrueif bitset1 and bitset2 have any common bits. (That is, if bitset1ANDbitset2 is not the empty set.)BIT_CMP()β returnstrueif bitset1 is NOT equal to bitset2. Not me pointing out the double negative, but yes,truemeans they're different πBIT_OR()β sets bits present in src in dst. (It is thebitset(9)equivalent of the scalar:dst |= src.)BIT_OR_ATOMIC()is similar, but sets bits in the component machine words in dst atomically. (That is, if dst is composed of multiple machine words,BIT_OR_ATOMIC()performs multiple individually atomic operations.)BIT_OR2()β computes src1 bitwise or src2 and assigns the result to dst. (It is thebitset(9)equivalent of the scalar:dst = src1 | src2.)BIT_AND()β clears bits absent from src from dst. (It is thebitset(9)equivalent of the scalar:dst &= src.)BIT_AND_ATOMIC()is similar, with the same atomic semantics asBIT_OR_ATOMIC().BIT_AND2()β computes src1 bitwise and src2 and assigns the result to dst. (It is thebitset(9)equivalent of the scalar:dst = src1 & src2.)BIT_NAND()β clears bits set in src from dst. (It is thebitset(9)equivalent of the scalar:dst &= ~ src.)BIT_NAND2()β computes src1 bitwise and not src2 and assigns the result to dst. (It is thebitset(9)equivalent of the scalar:dst = src1 & ~ src2.)BIT_XOR()β toggles bits set in src in dst. (It is thebitset(9)equivalent of the scalar:dst ^= src.) π₯BIT_XOR2()β computes src1 bitwise exclusive or src2 and assigns the result to dst. (It is thebitset(9)equivalent of the scalar:dst = src1 ^ src2.)
π BITSET_T_INITIALIZER EXAMPLE π
BITSET_DEFINE(_myset, MYSETSIZE);
struct _myset myset;
/* Initialize myset to filled (all bits set) */
myset = BITSET_T_INITIALIZER(BITSET_FSET(__bitset_words(MYSETSIZE)));
/* Initialize myset to only the lowest bit set */
myset = BITSET_T_INITIALIZER(0x1);
Look at that. Two whole moods: all the bits, or exactly one bit. Main character energy either way π
π SEE ALSO π
π HISTORY π
The bitset(9) macros first appeared in FreeBSD 10.0 in January 2014. They were MFCed to FreeBSD 9.3, released in July 2014. A glow-up so good it got backported π
This manual page first appeared in FreeBSD 11.0.
π«Ά AUTHORS π«Ά
The bitset(9) macros were generalized and pulled out of <sys/cpuset.h> as <sys/_bitset.h> and <sys/bitset.h> by Attilio Rao <attilio@FreeBSD.org>. This manual page was written by Conrad Meyer <cem@FreeBSD.org>. Legends, honestly πΈ
π CAVEATS π
- The
SETSIZEargument to all of these macros must match the value given toBITSET_DEFINE(). Fr fr. Don't mix and match, bestie, that's not a vibe. π - Unlike every other reference to individual set members, which are zero-indexed,
BIT_FFS()andBIT_FLS()return a one-indexed result (or zero if the set is empty). Yes this is the trap. Yes I'm telling you twice. Yes you'll still forget. π
Debian, July 7, 2017 β that's the whole source/version + date receipt, and this page is officially done. Byeee, go set some bits π¦β¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2015 Conrad Meyer <cem@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms