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