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