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