xref: /netbsd-src/share/man/man3/bitstring.3 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\"	$NetBSD: bitstring.3,v 1.16 2015/11/20 20:41:58 christos 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 November 20, 2015
35.Dt BITSTRING 3
36.Os
37.Sh NAME
38.Nm bit_alloc ,
39.Nm bit_clear ,
40.Nm bit_decl ,
41.Nm bit_ffc ,
42.Nm bit_ffs ,
43.Nm bit_nclear ,
44.Nm bit_nset ,
45.Nm bit_set ,
46.Nm bitstr_size ,
47.Nm bit_test
48.Nd bit-string manipulation macros
49.Sh SYNOPSIS
50.In bitstring.h
51.Ft bitstr_t *
52.Fn bit_decl "bitstr_t *name" "size_t nbits"
53.Ft bitstr_t *
54.Fn bit_alloc "size_t nbits"
55.Fn bit_clear "bitstr_t *name" "size_t bit"
56.Fn bit_ffc "const bitstr_t *name" "size_t nbits" "int *value"
57.Fn bit_ffs "const bitstr_t *name" "size_t nbits" "int *value"
58.Fn bit_nclear "bitstr_t *name" "size_t start" "size_t stop"
59.Fn bit_nset "bitstr_t *name" "size_t start" "size_t stop"
60.Fn bit_set "bitstr_t *name" "size_t bit"
61.Fn bitstr_size "size_t nbits"
62.Fn bit_test "const bitstr_t *name" "size_t 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 non-zero if the zero-based numbered bit
116.Fa bit
117of bit string
118.Fa name
119is set, and 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 EXAMPLES
149.Bd -literal -offset indent
150#include \*[Lt]limits.h\*[Gt]
151#include \*[Lt]bitstring.h\*[Gt]
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
161void
162make_lpr_available(void)
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 bitmap 3 ,
177.Xr calloc 3 ,
178.Xr setbit 9
179.Sh HISTORY
180The
181.Nm bitstring
182functions first appeared in
183.Bx 4.4 .
184