Merge branch 'vendor/GREP'
[dragonfly.git] / share / man / man3 / bitstring.3
1 .\" Copyright (c) 1989, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Paul Vixie.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
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. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed by the University of
17 .\"     California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\"    may be used to endorse or promote products derived from this software
20 .\"    without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\"
34 .\"     @(#)bitstring.3 8.1 (Berkeley) 7/19/93
35 .\" $FreeBSD: src/share/man/man3/bitstring.3,v 1.6.2.5 2001/12/17 11:30:11 ru Exp $
36 .\" $DragonFly: src/share/man/man3/bitstring.3,v 1.2 2003/06/17 04:36:58 dillon Exp $
37 .\"
38 .Dd July 19, 1993
39 .Dt BITSTRING 3
40 .Os
41 .Sh NAME
42 .Nm bit_alloc ,
43 .Nm bit_clear ,
44 .Nm bit_decl ,
45 .Nm bit_ffs ,
46 .Nm bit_fls ,
47 .Nm bit_nclear ,
48 .Nm bit_nset ,
49 .Nm bit_nsearch ,
50 .Nm bit_set ,
51 .Nm bitstr_size ,
52 .Nm bit_test
53 .Nd bit-string manipulation macros
54 .Sh SYNOPSIS
55 .In bitstring.h
56 .Ft bitstr_t *
57 .Fn bit_alloc "int nbits"
58 .Ft void
59 .Fn bit_decl "bitstr_t *name" "int nbits"
60 .Ft void
61 .Fn bit_clear "bitstr_t *name" "int bit"
62 .Ft void
63 .Fn bit_ffc "bitstr_t *name" "int nbits" "int *value"
64 .Ft void
65 .Fn bit_ffs "bitstr_t *name" "int nbits" "int *value"
66 .Ft void
67 .Fn bit_fls "bitstr_t *name" "int nbits" "int *value"
68 .Ft void
69 .Fn bit_nclear "bitstr_t *name" "int start" "int stop"
70 .Ft void
71 .Fn bit_nset "bitstr_t *name" "int start" "int stop"
72 .Ft void
73 .Fn bit_nsearch "bitstr_t *name" "int nbits" "int *value" "int len"
74 .Ft void
75 .Fn bit_set "bitstr_t *name" "int bit"
76 .Ft int
77 .Fn bitstr_size "int nbits"
78 .Ft int
79 .Fn bit_test "bitstr_t *name" "int bit"
80 .Sh DESCRIPTION
81 These macros operate on strings of bits.
82 .Pp
83 The macro
84 .Fn bit_alloc
85 returns a pointer of type
86 .Dq Fa "bitstr_t *"
87 to sufficient space to store
88 .Fa nbits
89 bits, or
90 .Dv NULL
91 if no space is available.
92 .Pp
93 The macro
94 .Fn bit_decl
95 allocates sufficient space to store
96 .Fa nbits
97 bits on the stack.
98 .Pp
99 The macro
100 .Fn bitstr_size
101 returns the number of elements of type
102 .Fa bitstr_t
103 necessary to store
104 .Fa nbits
105 bits.
106 This is useful for copying bit strings.
107 .Pp
108 The macros
109 .Fn bit_clear
110 and
111 .Fn bit_set
112 clear or set the zero-based numbered bit
113 .Fa bit ,
114 in the bit string
115 .Ar name .
116 .Pp
117 The
118 .Fn bit_nset
119 and
120 .Fn bit_nclear
121 macros
122 set or clear the zero-based numbered bits from
123 .Fa start
124 through
125 .Fa stop
126 in the bit string
127 .Ar name .
128 .Pp
129 The
130 .Fn bit_test
131 macro
132 evaluates to non-zero if the zero-based numbered bit
133 .Fa bit
134 of bit string
135 .Fa name
136 is set, and zero otherwise.
137 .Pp
138 The
139 .Fn bit_ffs
140 macro
141 stores in the location referenced by
142 .Fa value
143 the zero-based number of the first bit set in the array of
144 .Fa nbits
145 bits referenced by
146 .Fa name .
147 If no bits are set, the location referenced by
148 .Fa value
149 is set to \-1.
150 .Pp
151 The macro
152 .Fn bit_ffc
153 stores in the location referenced by
154 .Fa value
155 the zero-based number of the first bit not set in the array of
156 .Fa nbits
157 bits referenced by
158 .Fa name .
159 If all bits are set, the location referenced by
160 .Fa value
161 is set to \-1.
162 .Pp
163 The macro
164 .Fn bit_fls
165 stores in the location referenced by
166 .Fa value
167 the zero-based number of the highest bit set in array of
168 .Fa nbits
169 bits referenced by
170 .Fa name .
171 .Pp
172 The macro
173 .Fn bit_nsearch
174 stores in the location referenced by
175 .Fa value
176 the zero-based index of a span of clear bits of length
177 .Fa len
178 in the array of
179 .Fa nbits
180 bits referenced by
181 .Fa name .
182 .Pp
183 The arguments to these macros are evaluated only once and may safely
184 have side effects.
185 .Sh EXAMPLES
186 .Bd -literal -offset indent
187 #include <limits.h>
188 #include <bitstring.h>
189
190 \&...
191 #define LPR_BUSY_BIT            0
192 #define LPR_FORMAT_BIT          1
193 #define LPR_DOWNLOAD_BIT        2
194 \&...
195 #define LPR_AVAILABLE_BIT       9
196 #define LPR_MAX_BITS            10
197
198 make_lpr_available()
199 {
200         bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
201         ...
202         bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
203         ...
204         if (!bit_test(bitlist, LPR_BUSY_BIT)) {
205                 bit_clear(bitlist, LPR_FORMAT_BIT);
206                 bit_clear(bitlist, LPR_DOWNLOAD_BIT);
207                 bit_set(bitlist, LPR_AVAILABLE_BIT);
208         }
209 }
210 .Ed
211 .Sh SEE ALSO
212 .Xr malloc 3
213 .Sh HISTORY
214 The
215 .Nm bitstring
216 functions first appeared in
217 .Bx 4.4 .
218 The
219 .Fn bit_fls
220 and
221 .Fn bit_nsearch
222 routines first appeared in
223 .Dx 2.9 .