1 .\" Copyright (c) 1989, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" Copyright (c) 2014,2016 Spectra Logic Corporation
31 .\" All rights reserved.
33 .\" Redistribution and use in source and binary forms, with or without
34 .\" modification, are permitted provided that the following conditions
36 .\" 1. Redistributions of source code must retain the above copyright
37 .\" notice, this list of conditions, and the following disclaimer,
38 .\" without modification.
39 .\" 2. Redistributions in binary form must reproduce at minimum a disclaimer
40 .\" substantially similar to the "NO WARRANTY" disclaimer below
41 .\" ("Disclaimer") and any redistribution must be conditioned upon
42 .\" including a substantially similar Disclaimer requirement for further
43 .\" binary redistribution.
46 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 .\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
49 .\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
50 .\" HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55 .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 .\" POSSIBILITY OF SUCH DAMAGES.
58 .\" @(#)bitstring.3 8.1 (Berkeley) 7/19/93
85 .Nd bit-string manipulation functions and macros
89 .Fn bit_alloc "int nbits"
91 .Fn bit_decl "bitstr_t *name" "int nbits"
93 .Fn bit_clear "bitstr_t *name" "int bit"
95 .Fn bit_count "bitstr_t *name" "int count" "int nbits" "int *value"
97 .Fn bit_ffc "bitstr_t *name" "int nbits" "int *value"
99 .Fn bit_ffs "bitstr_t *name" "int nbits" "int *value"
101 .Fn bit_ffc_at "bitstr_t *name" "int start" "int nbits" "int *value"
103 .Fn bit_ffs_at "bitstr_t *name" "int start" "int nbits" "int *value"
105 .Fn bit_ff_at "bitstr_t *name" "int start" "int nbits" "int match" "int *value"
107 .Fn bit_ffc_area "bitstr_t *name" "int nbits" "int size" "int *value"
109 .Fn bit_ffs_area "bitstr_t *name" "int nbits" "int size" "int *value"
111 .Fn bit_ffc_area_at "bitstr_t *name" "int start" "int nbits" "int size" "int *value"
113 .Fn bit_ffs_area_at "bitstr_t *name" "int start" "int nbits" "int size" "int *value"
115 .Fn bit_ff_area_at "bitstr_t *name" "int start" "int nbits" "int size" "int match" "int *value"
116 .Fn bit_foreach "bitstr_t *name" "int nbits" "int var"
117 .Fn bit_foreach_at "bitstr_t *name" "int start" "int nbits" "int var"
118 .Fn bit_foreach_unset "bitstr_t *name" "int nbits" "int var"
119 .Fn bit_foreach_unset_at "bitstr_t *name" "int start" "int nbits" "int var"
121 .Fn bit_nclear "bitstr_t *name" "int start" "int stop"
123 .Fn bit_nset "bitstr_t *name" "int start" "int stop"
125 .Fn bit_ntest "bitstr_t *name" "int start" "int stop" "int match"
127 .Fn bit_set "bitstr_t *name" "int bit"
129 .Fn bitstr_size "int nbits"
131 .Fn bit_test "bitstr_t *name" "int bit"
133 These macros operate on strings of bits.
137 returns a pointer of type
139 to sufficient space to store
143 if no space is available.
144 If successful, the returned bit string is initialized with all bits cleared.
148 declares a bit string with sufficient space to store
152 may be used to include statically sized bit strings in structure
153 definitions or to create bit strings on the stack.
154 Users of this macro are responsible for initialization of the bit string,
155 typically via a global initialization of the containing struct or use of the
163 returns the number of bytes necessary to store
166 This is useful for copying bit strings.
172 clear or set the zero-based numbered bit
182 set or clear the zero-based numbered bits from
192 evaluates to non-zero if the zero-based numbered bit
196 is set, and zero otherwise.
201 evaluates to non-zero if the zero-based numbered bits from
212 stores in the location referenced by
214 the zero-based number of the first bit not set in the array of
218 If all bits are set, the location referenced by
225 stores in the location referenced by
227 the zero-based number of the first bit set in the array of
231 If no bits are set, the location referenced by
237 stores in the location referenced by
239 the zero-based number of the first bit not set in the array of
243 at or after the zero-based bit index
245 If all bits at or after
247 are set, the location referenced by
254 stores in the location referenced by
256 the zero-based number of the first bit set in the array of
260 at or after the zero-based bit index
262 If no bits are set after
264 the location referenced by
271 stores in the location referenced by
273 the zero-based number of the first bit in the array of
277 at or after the zero-based bit index
283 match that value, the location referenced by
289 function stores in the location referenced by
291 the zero-based number of the first bit beginning a sequence of unset bits of
294 unset bits in the array of
298 If no sequence of contiguous unset bits of the specified
300 can be found, the location referenced by
306 function stores in the location referenced by
308 the zero-based number of the first bit beginning a sequence of set bits of
311 set bits in the array of
315 If no sequence of contiguous set bits of the specified
317 can be found, the location referenced by
323 function stores in the location referenced by
325 the zero-based number of the first bit beginning a sequence of unset bits of
328 unset bits in the array of
332 at or after the zero-based bit index
334 If no sequence of contiguous unset bits of the specified
336 can be found at or after
338 the location referenced by
344 function stores in the location referenced by
346 the zero-based number of the first bit beginning a sequence of set bits of
349 set bits in the array of
353 at or after the zero-based bit index
355 If no sequence of contiguous set bits of the specified
357 can be found at or after
359 the location referenced by
365 function stores in the location referenced by
367 the zero-based number of the first bit beginning a sequence of bits of
374 at or after the zero-based bit index
376 in which all bits have the value
378 If no sequence of contiguous such bits of the specified
380 can be found at or after
382 the location referenced by
388 function stores in the location referenced by
390 the number of bits set in the array of
394 at or after the zero-based bit index
399 traverses all set bits in the array of
403 in the forward direction, assigning each location in turn to
408 traverses all set bits in the array of
412 in the forward direction at or after the zero-based bit index
414 assigning each location in turn to
418 .Fn bit_foreach_unset
419 traverses all unset bits in the array of
423 in the forward direction, assigning each location in turn to
427 .Fn bit_foreach_unset_at
428 traverses all unset bits in the array of
432 in the forward direction at or after the zero-based bit index
434 assigning each location in turn to
437 The arguments in bit string macros are evaluated only once and may safely
440 .Bd -literal -offset indent
442 #include <bitstring.h>
445 #define LPR_BUSY_BIT 0
446 #define LPR_FORMAT_BIT 1
447 #define LPR_DOWNLOAD_BIT 2
449 #define LPR_AVAILABLE_BIT 9
450 #define LPR_MAX_BITS 10
454 bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
456 bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
458 if (!bit_test(bitlist, LPR_BUSY_BIT)) {
459 bit_clear(bitlist, LPR_FORMAT_BIT);
460 bit_clear(bitlist, LPR_DOWNLOAD_BIT);
461 bit_set(bitlist, LPR_AVAILABLE_BIT);
471 functions first appeared in