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