386BSD 0.1 development
[unix-history] / usr / othersrc / share / man / man3 / bitstring.3
CommitLineData
a8856877
WJ
1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
2.\" 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 5.4 (Berkeley) 4/19/91
35.\"
36.Dd April 19, 1991
37.Dt BITSTRING 3
38.Os BSD 4
39.Sh NAME
40.Nm bit_alloc ,
41.Nm bit_clear ,
42.Nm bit_decl ,
43.Nm bit_ffs ,
44.Nm bit_nclear ,
45.Nm bit_nset,
46.Nm bit_set ,
47.Nm bitstr_size ,
48.Nm bit_test
49.Nd bit-string manipulation macros
50.Sh SYNOPSIS
51.Fd #include <bitstring.h>
52.Ft bitstr_t *
53.Fn bit_alloc "int nbits"
54.Fn bit_decl "bit_str name" "int nbits"
55.Fn bit_clear "bit_str name" "int bit"
56.Fn bit_ffc "bit_str name" "int nbits" "int *value"
57.Fn bit_ffs "bit_str name" "int nbits" "int *value"
58.Fn bit_nclear "bit_str name" "int start" "int stop"
59.Fn bit_nset "bit_str name" "int start" "int stop"
60.Fn bit_set "bit_str name" "int bit"
61.Fn bitstr_size "int nbits"
62.Fn bit_test "bit_str name" "int bit"
63.Sh DESCRIPTION
64These macros operate on strings of bits.
65.Pp
66The macro
67.Fn bit_alloc
68returns a pointer of type
69.Dq Fa "bitstr_t *"
70to sufficient space to store
71.Fa nbits
72bits, or
73.Dv NULL
74if no space is available.
75.Pp
76The macro
77.Fn bit_decl
78allocates sufficient space to store
79.Fa nbits
80bits on the stack.
81.Pp
82The macro
83.Fn bitstr_size
84returns the number of elements of type
85.Fa bitstr_t
86necessary to store
87.Fa nbits
88bits.
89This is useful for copying bit strings.
90.Pp
91The macros
92.Fn bit_clear
93and
94.Fn bit_set
95clear or set the zero-based numbered bit
96.Fa bit ,
97in the bit string
98.Ar name .
99.Pp
100The
101.Fn bit_nset
102and
103.Fn bit_nclear
104macros
105set or clear the zero-based numbered bits from
106.Fa start
107to
108.Fa stop
109in the bit string
110.Ar name .
111.Pp
112The
113.Fn bit_test
114macro
115evaluates to zero if the zero-based numbered bit
116.Fa bit
117of bit string
118.Fa name
119is set, and non-zero otherwise.
120.Pp
121The
122.Fn bit_ffs
123macro
124stores in the location referenced by
125.Fa value
126the zero-based number of the first bit set in the array of
127.Fa nbits
128bits referenced by
129.Fa name .
130If no bits are set, the location referenced by
131.Fa value
132is set to \-1.
133.Pp
134The macro
135.Fn bit_ffc
136stores in the location referenced by
137.Fa value
138the zero-based number of the first bit not set in the array of
139.Fa nbits
140bits referenced by
141.Fa name .
142If all bits are set, the location referenced by
143.Fa value
144is set to \-1.
145.Pp
146The arguments to these macros are evaluated only once and may safely
147have side effects.
148.Sh EXAMPLE
149.Bd -literal -offset indent
150#include <limits.h>
151#include <bitstring.h>
152
153...
154#define LPR_BUSY_BIT 0
155#define LPR_FORMAT_BIT 1
156#define LPR_DOWNLOAD_BIT 2
157...
158#define LPR_AVAILABLE_BIT 9
159#define LPR_MAX_BITS 10
160
161make_lpr_available()
162{
163 bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
164 ...
165 bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
166 ...
167 if (!bit_test(bitlist, LPR_BUSY_BIT)) {
168 bit_clear(bitlist, LPR_FORMAT_BIT);
169 bit_clear(bitlist, LPR_DOWNLOAD_BIT);
170 bit_set(bitlist, LPR_AVAILABLE_BIT);
171 }
172}
173.Ed
174.Sh SEE ALSO
175.Xr malloc 3
176.Sh HISTORY
177The
178.Nm
179functions are
180.Ud .