---
name: "bitstring"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/bit_alloc"
---

# bitstring(3)

bit-string manipulation functions and macros

Part of [bit\_alloc(3)](/bit_alloc/3.md).

## NAME

**bit\_alloc**, **bit\_clear**, **bit\_count**, **bit\_decl**, **bit\_ffc**, **bit\_ffs**, **bit\_ff\_at**, **bit\_ffc\_at**, **bit\_ffs\_at**, **bit\_ffc\_area**, **bit\_ffs\_area**, **bit\_ff\_area\_at**, **bit\_ffc\_area\_at**, **bit\_ffs\_area\_at**, **bit\_nclear**, **bit\_nset**, **bit\_ntest**, **bit\_set**, **bit\_test**, **bitstr\_size** – bit-string manipulation functions and macros

## SYNOPSIS

**#include \<bitstring.h\>**

*bitstr\_t* *\**\

**bit\_alloc**(*size\_t nbits*);

*void*\

**bit\_decl**(*bitstr\_t \*name*, *size\_t nbits*);

*void*\

**bit\_clear**(*bitstr\_t \*name*, *size\_t bit*);

*void*\

**bit\_count**(*bitstr\_t \*name*, *size\_t count*, *size\_t nbits*, *ssize\_t \*value*);

*void*\

**bit\_ffc**(*bitstr\_t \*name*, *size\_t nbits*, *ssize\_t \*value*);

*void*\

**bit\_ffs**(*bitstr\_t \*name*, *size\_t nbits*, *ssize\_t \*value*);

*void*\

**bit\_ffc\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *ssize\_t \*value*);

*void*\

**bit\_ffs\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *ssize\_t \*value*);

*void*\

**bit\_ff\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *int match*, *ssize\_t \*value*);

*void*\

**bit\_ffc\_area**(*bitstr\_t \*name*, *size\_t nbits*, *size\_t size*, *ssize\_t \*value*);

*void*\

**bit\_ffs\_area**(*bitstr\_t \*name*, *size\_t nbits*, *size\_t size*, *ssize\_t \*value*);

*void*\

**bit\_ffc\_area\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *size\_t size*, *ssize\_t \*value*);

*void*\

**bit\_ffs\_area\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *size\_t size*, *ssize\_t \*value*);

*void*\

**bit\_ff\_area\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *size\_t size*, *int match*, *ssize\_t \*value*);

**bit\_foreach**(*bitstr\_t \*name*, *size\_t nbits*, *size\_t var*);

**bit\_foreach\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *size\_t var*);

**bit\_foreach\_unset**(*bitstr\_t \*name*, *size\_t nbits*, *size\_t var*);

**bit\_foreach\_unset\_at**(*bitstr\_t \*name*, *size\_t start*, *size\_t nbits*, *size\_t var*);

*void*\

**bit\_nclear**(*bitstr\_t \*name*, *size\_t start*, *size\_t stop*);

*void*\

**bit\_nset**(*bitstr\_t \*name*, *size\_t start*, *size\_t stop*);

*int*\

**bit\_ntest**(*bitstr\_t \*name*, *size\_t start*, *size\_t stop*, *int match*);

*void*\

**bit\_set**(*bitstr\_t \*name*, *size\_t bit*);

*int*\

**bitstr\_size**(*size\_t nbits*);

*int*\

**bit\_test**(*bitstr\_t \*name*, *size\_t bit*);

## DESCRIPTION

These macros operate on strings of bits.

The function **bit\_alloc**() returns a pointer of type “*bitstr\_t \**” to sufficient space to store *nbits* bits, or NULL if no space is available. If successful, the returned bit string is initialized with all bits cleared.

The macro **bit\_decl**() declares a bit string with sufficient space to store *nbits* bits. **bit\_decl**() may be used to include statically sized bit strings in structure definitions or to create bit strings on the stack. Users of this macro are responsible for initialization of the bit string, typically via a global initialization of the containing struct or use of the **bit\_nset**() or **bin\_nclear**() functions.

The macro **bitstr\_size**() returns the number of bytes necessary to store *nbits* bits. This is useful for copying bit strings.

The functions **bit\_clear**() and **bit\_set**() clear or set the zero-based numbered bit *bit*, in the bit string *name*.

The **bit\_nset**() and **bit\_nclear**() functions set or clear the zero-based numbered bits from *start* through *stop* in the bit string *name*.

The **bit\_test**() function evaluates to non-zero if the zero-based numbered bit *bit* of bit string *name* is set, and zero otherwise.

The **bit\_ntest**() function evaluates to non-zero if the zero-based numbered bits from *start* through *stop* in the bit string *name* all have the value *match*.

The function **bit\_ffc**() stores in the location referenced by *value* the zero-based number of the first bit not set in the array of *nbits* bits referenced by *name*. If all bits are set, the location referenced by *value* is set to -1.

The **bit\_ffs**() function stores in the location referenced by *value* the zero-based number of the first bit set in the array of *nbits* bits referenced by *name*. If no bits are set, the location referenced by *value* is set to -1.

The function **bit\_ffc\_at**() stores in the location referenced by *value* the zero-based number of the first bit not set in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start*. If all bits at or after *start* are set, the location referenced by *value* is set to -1.

The **bit\_ffs\_at**() function stores in the location referenced by *value* the zero-based number of the first bit set in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start*. If no bits are set after *start*, the location referenced by *value* is set to -1.

The **bit\_ff\_at**() function stores in the location referenced by *value* the zero-based number of the first bit in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start* that has value *match*. If no bits after *start* match that value, the location referenced by *value* is set to -1.

The **bit\_ffc\_area**() function stores in the location referenced by *value* the zero-based number of the first bit beginning a sequence of unset bits of at least *size* unset bits in the array of *nbits* bits referenced by *name*. If no sequence of contiguous unset bits of the specified *size* can be found, the location referenced by *value* is set to -1.

The **bit\_ffs\_area**() function stores in the location referenced by *value* the zero-based number of the first bit beginning a sequence of set bits of at least *size* set bits in the array of *nbits* bits referenced by *name*. If no sequence of contiguous set bits of the specified *size* can be found, the location referenced by *value* is set to -1.

The **bit\_ffc\_area\_at**() function stores in the location referenced by *value* the zero-based number of the first bit beginning a sequence of unset bits of at least *size* unset bits in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start*. If no sequence of contiguous unset bits of the specified *size* can be found at or after *start*, the location referenced by *value* is set to -1.

The **bit\_ffs\_area\_at**() function stores in the location referenced by *value* the zero-based number of the first bit beginning a sequence of set bits of at least *size* set bits in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start*. If no sequence of contiguous set bits of the specified *size* can be found at or after *start*, the location referenced by *value* is set to -1.

The **bit\_ff\_area\_at**() function stores in the location referenced by *value* the zero-based number of the first bit beginning a sequence of bits of at least *size* bits in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start* in which all bits have the value *match*. If no sequence of contiguous such bits of the specified *size* can be found at or after *start*, the location referenced by *value* is set to -1.

The **bit\_count**() function stores in the location referenced by *value* the number of bits set in the array of *nbits* bits referenced by *name*, at or after the zero-based bit index *start*.

The macro **bit\_foreach**() traverses all set bits in the array of *nbits* referenced by *name* in the forward direction, assigning each location in turn to *var*.

The macro **bit\_foreach\_at**() traverses all set bits in the array of *nbits* referenced by *name* in the forward direction at or after the zero-based bit index *start*, assigning each location in turn to *var*.

The macro **bit\_foreach\_unset**() traverses all unset bits in the array of *nbits* referenced by *name* in the forward direction, assigning each location in turn to *var*.

The macro **bit\_foreach\_unset\_at**() traverses all unset bits in the array of *nbits* referenced by *name* in the forward direction at or after the zero-based bit index *start*, assigning each location in turn to *var*.

The arguments in bit string macros are evaluated only once and may safely have side effects.

## EXAMPLES

```
#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)](/malloc/3.md), [stdbit(3)](/stdbit/3.md), [bitset(9)](/bitset/9.md)

## HISTORY

The **bitstring** functions first appeared in 4.4BSD.
