---
name: "BIT_SET_ATOMIC"
section: "9freebsd"
platform: "linux"
package: "freebsd-manpages"
version: "12.2-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/BITSET_DEFINE/9freebsd"
---

# BIT\_SET\_ATOMIC(9freebsd)

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 – bitset manipulation macros

Part of [BITSET\_DEFINE(9freebsd)](/BITSET_DEFINE/9freebsd.md).

## NAME

**[bitset(9)](/bitset/9.md)** — **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** – 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*);

*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)](/bitset/9.md)** family of macros provide 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⟩.

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\_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\_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\_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)](/ffs/3.md), to use the non-zero result of **BIT\_FFS**() as a *bit* index parameter to any other **[bitset(9)](/bitset/9.md)** macro, you must subtract one from the result.

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)](/fls/3.md), to use the non-zero result of **BIT\_FLS**() as a *bit* index parameter to any other **[bitset(9)](/bitset/9.md)** macro, you must subtract one from the result.

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)](/bitset/9.md)** 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)](/bitset/9.md)** equivalent of the scalar: *dst* = *src1* \| *src2*.)

The **BIT\_AND**() macro clears bits absent from *src* from *dst*. (It is the **[bitset(9)](/bitset/9.md)** 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)](/bitset/9.md)** equivalent of the scalar: *dst* = *src1* & *src2*.)

The **BIT\_NAND**() macro clears bits set in *src* from *dst*. (It is the **[bitset(9)](/bitset/9.md)** equivalent of the scalar: *dst* &= *~*, *src*.)

The **BIT\_NAND2**() macro computes *src1* bitwise and not *src2* and assigns the result to *dst*. (It is the **[bitset(9)](/bitset/9.md)** equivalent of the scalar: *dst* = *src1* & ~ *src2*.)

The **BIT\_XOR**() macro toggles bits set in *src* in *dst*. (It is the **[bitset(9)](/bitset/9.md)** 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)](/bitset/9.md)** 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);
```

## SEE ALSO

[bitstring(3)](/bitstring/3.md), [cpuset(9)](/cpuset/9.md)

## HISTORY

The **[bitset(9)](/bitset/9.md)** 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)](/bitset/9.md)** 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

The *SETSIZE* argument to all of these macros must match the value given to **BITSET\_DEFINE**().

Unlike every other reference to individual set members, which are zero-indexed, **BIT\_FFS**() and **BIT\_FLS**() return a one-indexed result (or zero if the set is empty).
