BITSET(9)
π BITSET(9) π β bitset manipulation macros
okay so bestie, welcome to bitset(9) from the Kernel Developer's Manual. This is the bitset macro fam, and they are lowkey iconic: flexible, efficient, and ready to hold your bits if the maximum size of the set is known at compilation. π
β¨
β¨ NAME β¨
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_FFS_AT, BIT_FLS, BIT_FOREACH_ISSET, BIT_FOREACH_ISCLR, BIT_COUNT, BIT_SUBSET, BIT_OVERLAP, BIT_CMP, BIT_OR, BIT_OR2, BIT_ORNOT, BIT_ORNOT2, BIT_AND, BIT_AND2, BIT_ANDNOT, BIT_ANDNOT2, BIT_XOR, BIT_XOR2, BIT_CLR_ATOMIC, BIT_SET_ATOMIC, BIT_SET_ATOMIC_ACQ, BIT_TEST_SET_ATOMIC, BIT_TEST_CLR_ATOMIC, BIT_AND_ATOMIC, BIT_OR_ATOMIC, BIT_COPY_STORE_REL β bitset manipulation macros
πΈ 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);
long
BIT_FFS(const SETSIZE, struct STRUCTNAME *bitset);
long
BIT_FFS_AT(const SETSIZE, struct STRUCTNAME *bitset, long start);
long
BIT_FLS(const SETSIZE, struct STRUCTNAME *bitset);
BIT_FOREACH_ISSET(const SETSIZE, size_t bit,
const struct STRUCTNAME *bitset);
BIT_FOREACH_ISCLR(const SETSIZE, size_t bit,
const struct STRUCTNAME *bitset);
long
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_ORNOT(const SETSIZE, struct STRUCTNAME *dst,
struct STRUCTNAME *src);
BIT_ORNOT2(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_ANDNOT(const SETSIZE, struct STRUCTNAME *dst,
struct STRUCTNAME *src);
BIT_ANDNOT2(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);
bool
BIT_TEST_SET_ATOMIC(const SETSIZE, size_t bit,
struct STRUCTNAME *bitset);
bool
BIT_TEST_CLR_ATOMIC(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);
#define _WANT_FREEBSD_BITSET
π DESCRIPTION π
okay so the bitset(9) family of macros provides a flexible and efficient bitset implementation if the maximum size of the set is known at compilation. Throughout this manual page, the name SETSIZE refers to the size of the bitset in bits. Individual bits in bitsets are referenced with indices zero through SETSIZE - 1. One example use of <sys/bitset.h> is <sys/cpuset.h>. π«Ά
These macros are meant to be used in the kernel and are visible if _KERNEL is defined when <sys/_bitset.h> or <sys/bitset.h> are included in a program. Userland programs must define _WANT_FREEBSD_BITSET before including these files to make the macros visible. iykyk.
BITSET_DEFINE()β defines a bitsetstruct STRUCTNAMEwith room to representSETSIZEbits. main character struct energy, honestly.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 theBITSET_T_INITIALIZER EXAMPLEsection. TheN_WORDSparameter toBITSET_FSET()should be:
__bitset_words(SETSIZE)
BIT_CLR()β clears bitbitin the bitset pointed to bybitset.BIT_CLR_ATOMIC()is identical, but the bit is cleared atomically.BIT_TEST_CLR_ATOMIC()atomically clears the bit and returns whether it was set.BIT_COPY()β copies the contents of the bitsetfromto the bitsetto.BIT_COPY_STORE_REL()is similar, but copies component machine words fromfromand writes them totowith atomic store with release semantics. (That is, iftois composed of multiple machine words,BIT_COPY_STORE_REL()performs multiple individually atomic operations.)BIT_ISSET()β returnstrueif the bitbitin the bitset pointed to bybitsetis set.BIT_SET()β sets bitbitin the bitset pointed to bybitset.BIT_SET_ATOMIC()is identical, but the bit is set atomically.BIT_SET_ATOMIC_ACQ()sets the bit with acquire semantics.BIT_TEST_SET_ATOMIC()atomically sets the bit and returns whether it was set.BIT_ZERO()β clears all bits inbitset.BIT_FILL()β sets all bits inbitset.BIT_SETOF()β clears all bits inbitsetbefore setting only bitbit.BIT_EMPTY()β returnstrueifbitsetis empty.BIT_ISFULLSET()β returnstrueifbitsetis full (all bits set).BIT_FFS()β returns the 1-index of the first (lowest) set bit inbitset, or zero ifbitsetis 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_FFS_AT()β returns the 1-index of the first (lowest) set bit inbitset, which is greater than the given 1-indexedstart, or zero if no bits inbitsetgreater thanstartare set.BIT_FLS()β returns the 1-index of the last (highest) set bit inbitset, or zero ifbitsetis 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.BIT_FOREACH_ISSET()β can be used to iterate over all set bits inbitset. The index variablebitmust have been declared with typeint, and upon each iterationbitis set to the index of successive set bits. The value ofbitafter the loop terminates is undefined. Similarly,BIT_FOREACH_ISCLR()iterates over all clear bits inbitset. In the loop body, the currently indexed bit may be set or cleared. However, setting or clearing bits other than the currently indexed bit does not guarantee that they will or will not be returned in subsequent iterations of the same loop.BIT_COUNT()β returns the total number of set bits inbitset.BIT_SUBSET()β returnstrueifneedleis a subset ofhaystack.BIT_OVERLAP()β returnstrueifbitset1andbitset2have any common bits. (That is, ifbitset1ANDbitset2is not the empty set.)BIT_CMP()β returnstrueifbitset1is NOT equal tobitset2.BIT_OR()β sets bits present insrcindst. (It is thebitset(9)equivalent of the scalar:dst |= src.)BIT_OR_ATOMIC()is similar, but sets bits in the component machine words indstatomically. (That is, ifdstis composed of multiple machine words,BIT_OR_ATOMIC()performs multiple individually atomic operations.)BIT_OR2()β computessrc1bitwise orsrc2and assigns the result todst. (It is thebitset(9)equivalent of the scalar:dst = src1 | src2.)BIT_ORNOT()β sets bits not insrcindst. (It is thebitset(9)equivalent of the scalar:dst |= ~ src.)BIT_ORNOT2()β computessrc1bitwise or notsrc2and assigns the result todst. (It is thebitset(9)equivalent of the scalar:dst = src1 | ~ src2.)BIT_AND()β clears bits absent fromsrcfromdst. (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()β computessrc1bitwise andsrc2and assigns the result todst. (It is thebitset(9)equivalent of the scalar:dst = src1 & src2.)BIT_ANDNOT()β clears bits set insrcfromdst. (It is thebitset(9)equivalent of the scalar:dst &= ~ src.)BIT_ANDNOT2()β computessrc1bitwise and notsrc2and assigns the result todst. (It is thebitset(9)equivalent of the scalar:dst = src1 & ~ src2.)BIT_XOR()β toggles bits set insrcindst. (It is thebitset(9)equivalent of the scalar:dst ^= src.)BIT_XOR2()β computessrc1bitwise exclusive orsrc2and assigns the result todst. (It is thebitset(9)equivalent of the scalar:dst = src1 ^ src2.)
π BITSET_T_INITIALIZER EXAMPLE π
okay so here's the example, and it's giving compile-time literal realness:
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);
π¦ 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.
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>.
π CAVEATS π
Okay bestie, caveat check β these are the little βgirlie pop, pay attentionβ notes, fr fr.
SETSIZEmatch-up era β theSETSIZEargument to all of these macros must match the value given toBITSET_DEFINE(). Like, all of them. No cap.- The indexing plot twist β unlike every other reference to individual set members, which are zero-indexed,
BIT_FFS(),BIT_FFS_AT(), andBIT_FLS()return a one-indexed result (or zero if the set is empty). So those three are giving different energy β iconic, but donβt mix it up. π - Userland header lore β in order to use the macros defined in
<sys/bitset.h>and<sys/_bitset.h>in userland programs,_WANT_FREEBSD_BITSEThas to be defined before including the header files. This requirement exists to prevent namespace pollution due to macros defined inbitset(9)in programs that include<sys/cpuset.h>or<sched.h>. Define it first, bestie β before the includes, iykyk. β¨
π«Ά thatβs the caveat section, and yes it ate.
Cute footer sign-off: source/version is Debian, date is September 20, 2021, and the page is BITSET(9) β periodt. π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2015 Conrad Meyer <cem@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms