Main index | Section 9 | Options |
#include <sys/_bitset.h>
#include <sys/bitset.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.
The BITSET_DEFINE() macro defines a bitset struct STRUCTNAME with room to represent SETSIZE bits.
The BITSET_T_INITIALIZER() macro allows one to initialize a bitset struct with a compile time literal value.
The BITSET_FSET() macro generates a compile time literal, usable by BITSET_T_INITIALIZER(), representing a full bitset (all bits set). For examples of BITSET_T_INITIALIZER() and BITSET_FSET() usage, see the BITSET_T_INITIALIZER EXAMPLE section. The N_WORDS parameter to BITSET_FSET() should be:
__bitset_words(SETSIZE)
The BIT_CLR() macro clears bit bit in the bitset pointed to by bitset. The BIT_CLR_ATOMIC() macro is identical, but the bit is cleared atomically. The BIT_TEST_CLR_ATOMIC() macro atomically clears the bit and returns whether it was set.
The BIT_COPY() macro 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.)
The BIT_ISSET() macro returns true if the bit bit in the bitset pointed to by bitset is set.
The BIT_SET() macro sets bit bit in the bitset pointed to by bitset. The BIT_SET_ATOMIC() macro is identical, but the bit is set atomically. The BIT_SET_ATOMIC_ACQ() macro sets the bit with acquire semantics. The BIT_TEST_SET_ATOMIC() macro atomically sets the bit and returns whether it was set.
The BIT_ZERO() macro clears all bits in bitset.
The BIT_FILL() macro sets all bits in bitset.
The BIT_SETOF() macro clears all bits in bitset before setting only bit bit.
The BIT_EMPTY() macro returns true if bitset is empty.
The BIT_ISFULLSET() macro returns true if bitset is full (all bits set).
The BIT_FFS() macro returns the 1-index of the first (lowest) set bit in bitset, or zero if bitset is empty. Like with ffs(3), to use the non-zero result of BIT_FFS() as a bit index parameter to any other bitset(9) macro, you must subtract one from the result.
The BIT_FFS_AT() macro returns the 1-index of the first (lowest) set bit in bitset, which is greater than the given 1-indexed start, or zero if no bits in bitset greater than start are set.
The BIT_FLS() macro returns the 1-index of the last (highest) set bit in bitset, or zero if bitset is empty. Like with fls(3), to use the non-zero result of BIT_FLS() as a bit index parameter to any other bitset(9) macro, you must subtract one from the result.
The BIT_FOREACH_ISSET() macro can be used to iterate over all set bits in bitset. The index variable bit must have been declared with type int, and upon each iteration bit is set to the index of successive set bits. The value of bit after the loop terminates is undefined. Similarly, BIT_FOREACH_ISCLR() iterates over all clear bits in bitset. 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.
The BIT_COUNT() macro returns the total number of set bits in bitset.
The BIT_SUBSET() macro returns true if needle is a subset of haystack.
The BIT_OVERLAP() macro returns true if bitset1 and bitset2 have any common bits. (That is, if bitset1 AND bitset2 is not the empty set.)
The BIT_CMP() macro returns true if bitset1 is NOT equal to bitset2.
The BIT_OR() macro sets bits present in src in dst. (It is the bitset(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.)
The BIT_OR2() macro computes src1 bitwise or src2 and assigns the result to dst. (It is the bitset(9) equivalent of the scalar: dst = src1 | src2, .)
The BIT_AND() macro clears bits absent from src from dst. (It is the bitset(9) equivalent of the scalar: dst &= src, .) BIT_AND_ATOMIC() is similar, with the same atomic semantics as BIT_OR_ATOMIC().
The BIT_AND2() macro computes src1 bitwise and src2 and assigns the result to dst. (It is the bitset(9) equivalent of the scalar: dst = src1 & src2, .)
The BIT_ANDNOT() macro clears bits set in src from dst. (It is the bitset(9) equivalent of the scalar: dst &= ~, src, .)
The BIT_ANDNOT2() macro computes src1 bitwise and not src2 and assigns the result to dst. (It is the bitset(9) equivalent of the scalar: dst = src1 & ~ src2, .)
The BIT_XOR() macro toggles bits set in src in dst. (It is the bitset(9) equivalent of the scalar: dst ^= src, .)
The BIT_XOR2() macro computes src1 bitwise exclusive or src2 and assigns the result to dst. (It is the bitset(9) equivalent of the scalar: dst = src1 ^ src2, .)
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);
This manual page first appeared in FreeBSD 11.0 .
Unlike every other reference to individual set members, which are zero-indexed, BIT_FFS(), BIT_FFS_AT() and BIT_FLS() return a one-indexed result (or zero if the set is empty).
In order to use the macros defined in <sys/bitset.h> and <sys/_bitset.h> in userland programs, _WANT_FREEBSD_BITSET has to be defined before including the header files. This requirements exists to prevent a name space pollution due to macros defined in bitset(9) in programs that include <sys/cpuset.h> or <sched.h>.
BITSET (9) | September 20, 2021 |
Main index | Section 9 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | This philosophy, in the hands of amateurs, leads to inexplicably mind-numbing botches like the existence of two programs, “head” and “tail,” which print the first part or the last part of a file, depending. Even though their operations are duals of one another, “head” and “tail” are different programs, written by different authors, and take different options! | ” |
— The Unix Haters' handbook |