xref: /freebsd/share/man/man3/bitstring.3 (revision 1b82e02f4d91dc0942e754fa6c08042537f13980)
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.
14dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors
15afe61c15SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
16afe61c15SRodney W. Grimes.\"    without specific prior written permission.
17afe61c15SRodney W. Grimes.\"
18afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21afe61c15SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28afe61c15SRodney W. Grimes.\" SUCH DAMAGE.
29afe61c15SRodney W. Grimes.\"
30*1b82e02fSAlan Somers.\" Copyright (c) 2014,2016 Spectra Logic Corporation
318907f744SAlan Somers.\" All rights reserved.
328907f744SAlan Somers.\"
338907f744SAlan Somers.\" Redistribution and use in source and binary forms, with or without
348907f744SAlan Somers.\" modification, are permitted provided that the following conditions
358907f744SAlan Somers.\" are met:
368907f744SAlan Somers.\" 1. Redistributions of source code must retain the above copyright
378907f744SAlan Somers.\"    notice, this list of conditions, and the following disclaimer,
388907f744SAlan Somers.\"    without modification.
398907f744SAlan Somers.\" 2. Redistributions in binary form must reproduce at minimum a disclaimer
408907f744SAlan Somers.\"    substantially similar to the "NO WARRANTY" disclaimer below
418907f744SAlan Somers.\"    ("Disclaimer") and any redistribution must be conditioned upon
428907f744SAlan Somers.\"    including a substantially similar Disclaimer requirement for further
438907f744SAlan Somers.\"    binary redistribution.
448907f744SAlan Somers.\"
458907f744SAlan Somers.\" NO WARRANTY
468907f744SAlan Somers.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
478907f744SAlan Somers.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
488907f744SAlan Somers.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
498907f744SAlan Somers.\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
508907f744SAlan Somers.\" HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
518907f744SAlan Somers.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
528907f744SAlan Somers.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
538907f744SAlan Somers.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
548907f744SAlan Somers.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
558907f744SAlan Somers.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
568907f744SAlan Somers.\" POSSIBILITY OF SUCH DAMAGES.
578907f744SAlan Somers.\"
58afe61c15SRodney W. Grimes.\"     @(#)bitstring.3	8.1 (Berkeley) 7/19/93
597f3dea24SPeter Wemm.\" $FreeBSD$
60afe61c15SRodney W. Grimes.\"
61*1b82e02fSAlan Somers.Dd May 23, 2016
62afe61c15SRodney W. Grimes.Dt BITSTRING 3
633d45e180SRuslan Ermilov.Os
64afe61c15SRodney W. Grimes.Sh NAME
65afe61c15SRodney W. Grimes.Nm bit_alloc ,
66afe61c15SRodney W. Grimes.Nm bit_clear ,
67*1b82e02fSAlan Somers.Nm bit_count ,
68afe61c15SRodney W. Grimes.Nm bit_decl ,
698907f744SAlan Somers.Nm bit_ffc ,
70afe61c15SRodney W. Grimes.Nm bit_ffs ,
718907f744SAlan Somers.Nm bit_ffc_at ,
728907f744SAlan Somers.Nm bit_ffs_at ,
73afe61c15SRodney W. Grimes.Nm bit_nclear ,
74afe61c15SRodney W. Grimes.Nm bit_nset ,
75afe61c15SRodney W. Grimes.Nm bit_set ,
768907f744SAlan Somers.Nm bit_test ,
778907f744SAlan Somers.Nm bitstr_size
788907f744SAlan Somers.Nd bit-string manipulation functions and macros
79afe61c15SRodney W. Grimes.Sh SYNOPSIS
8032eef9aeSRuslan Ermilov.In bitstring.h
81afe61c15SRodney W. Grimes.Ft bitstr_t *
82afe61c15SRodney W. Grimes.Fn bit_alloc "int nbits"
8369a6a1d7SBruce Evans.Ft void
8469a6a1d7SBruce Evans.Fn bit_decl "bitstr_t *name" "int nbits"
8569a6a1d7SBruce Evans.Ft void
8669a6a1d7SBruce Evans.Fn bit_clear "bitstr_t *name" "int bit"
8769a6a1d7SBruce Evans.Ft void
88*1b82e02fSAlan Somers.Fn bit_count "bitstr_t *name" "int count" "int nbits" "int *value"
89*1b82e02fSAlan Somers.Ft void
9069a6a1d7SBruce Evans.Fn bit_ffc "bitstr_t *name" "int nbits" "int *value"
9169a6a1d7SBruce Evans.Ft void
9269a6a1d7SBruce Evans.Fn bit_ffs "bitstr_t *name" "int nbits" "int *value"
9369a6a1d7SBruce Evans.Ft void
948907f744SAlan Somers.Fn bit_ffc_at "bitstr_t *name" "int start" "int nbits" "int *value"
958907f744SAlan Somers.Ft void
968907f744SAlan Somers.Fn bit_ffs_at "bitstr_t *name" "int start" "int nbits" "int *value"
978907f744SAlan Somers.Ft void
9869a6a1d7SBruce Evans.Fn bit_nclear "bitstr_t *name" "int start" "int stop"
9969a6a1d7SBruce Evans.Ft void
10069a6a1d7SBruce Evans.Fn bit_nset "bitstr_t *name" "int start" "int stop"
10169a6a1d7SBruce Evans.Ft void
10269a6a1d7SBruce Evans.Fn bit_set "bitstr_t *name" "int bit"
10369a6a1d7SBruce Evans.Ft int
104afe61c15SRodney W. Grimes.Fn bitstr_size "int nbits"
10569a6a1d7SBruce Evans.Ft int
10669a6a1d7SBruce Evans.Fn bit_test "bitstr_t *name" "int bit"
107afe61c15SRodney W. Grimes.Sh DESCRIPTION
108afe61c15SRodney W. GrimesThese macros operate on strings of bits.
109afe61c15SRodney W. Grimes.Pp
1108907f744SAlan SomersThe function
111afe61c15SRodney W. Grimes.Fn bit_alloc
112afe61c15SRodney W. Grimesreturns a pointer of type
113afe61c15SRodney W. Grimes.Dq Fa "bitstr_t *"
114afe61c15SRodney W. Grimesto sufficient space to store
115afe61c15SRodney W. Grimes.Fa nbits
116afe61c15SRodney W. Grimesbits, or
117afe61c15SRodney W. Grimes.Dv NULL
118afe61c15SRodney W. Grimesif no space is available.
1198907f744SAlan SomersIf successful, the returned bit string is initialized with all bits cleared.
120afe61c15SRodney W. Grimes.Pp
121afe61c15SRodney W. GrimesThe macro
122afe61c15SRodney W. Grimes.Fn bit_decl
1238907f744SAlan Somersdeclares a bit string with sufficient space to store
124afe61c15SRodney W. Grimes.Fa nbits
1258907f744SAlan Somersbits.
1268907f744SAlan Somers.Fn bit_decl
1278907f744SAlan Somersmay be used to include statically sized bit strings in structure
1288907f744SAlan Somersdefinitions or to create bit strings on the stack.
1298907f744SAlan SomersUsers of this macro are responsible for initialization of the bit string,
1308907f744SAlan Somerstypically via a global initialization of the containing struct or use of the
1318907f744SAlan Somers.Fn bit_nset
1328907f744SAlan Somersor
1338907f744SAlan Somers.Fn bin_nclear
1348907f744SAlan Somersfunctions.
135afe61c15SRodney W. Grimes.Pp
136afe61c15SRodney W. GrimesThe macro
137afe61c15SRodney W. Grimes.Fn bitstr_size
1388907f744SAlan Somersreturns the number of bytes necessary to store
139afe61c15SRodney W. Grimes.Fa nbits
140afe61c15SRodney W. Grimesbits.
141afe61c15SRodney W. GrimesThis is useful for copying bit strings.
142afe61c15SRodney W. Grimes.Pp
1438907f744SAlan SomersThe functions
144afe61c15SRodney W. Grimes.Fn bit_clear
145afe61c15SRodney W. Grimesand
146afe61c15SRodney W. Grimes.Fn bit_set
147afe61c15SRodney W. Grimesclear or set the zero-based numbered bit
148afe61c15SRodney W. Grimes.Fa bit ,
149afe61c15SRodney W. Grimesin the bit string
150afe61c15SRodney W. Grimes.Ar name .
151afe61c15SRodney W. Grimes.Pp
152afe61c15SRodney W. GrimesThe
153afe61c15SRodney W. Grimes.Fn bit_nset
154afe61c15SRodney W. Grimesand
155afe61c15SRodney W. Grimes.Fn bit_nclear
1568907f744SAlan Somersfunctions
157afe61c15SRodney W. Grimesset or clear the zero-based numbered bits from
158afe61c15SRodney W. Grimes.Fa start
1590b607240SJohn-Mark Gurneythrough
160afe61c15SRodney W. Grimes.Fa stop
161afe61c15SRodney W. Grimesin the bit string
162afe61c15SRodney W. Grimes.Ar name .
163afe61c15SRodney W. Grimes.Pp
164afe61c15SRodney W. GrimesThe
165afe61c15SRodney W. Grimes.Fn bit_test
1668907f744SAlan Somersfunction
167afe61c15SRodney W. Grimesevaluates to non-zero if the zero-based numbered bit
168afe61c15SRodney W. Grimes.Fa bit
169afe61c15SRodney W. Grimesof bit string
170afe61c15SRodney W. Grimes.Fa name
171afe61c15SRodney W. Grimesis set, and zero otherwise.
172afe61c15SRodney W. Grimes.Pp
1738907f744SAlan SomersThe function
174afe61c15SRodney W. Grimes.Fn bit_ffc
175afe61c15SRodney W. Grimesstores in the location referenced by
176afe61c15SRodney W. Grimes.Fa value
177afe61c15SRodney W. Grimesthe zero-based number of the first bit not set in the array of
178afe61c15SRodney W. Grimes.Fa nbits
179afe61c15SRodney W. Grimesbits referenced by
180afe61c15SRodney W. Grimes.Fa name .
181afe61c15SRodney W. GrimesIf all bits are set, the location referenced by
182afe61c15SRodney W. Grimes.Fa value
183afe61c15SRodney W. Grimesis set to \-1.
184afe61c15SRodney W. Grimes.Pp
1858907f744SAlan SomersThe
1868907f744SAlan Somers.Fn bit_ffs
1878907f744SAlan Somersfunction
1888907f744SAlan Somersstores in the location referenced by
1898907f744SAlan Somers.Fa value
1908907f744SAlan Somersthe zero-based number of the first bit set in the array of
1918907f744SAlan Somers.Fa nbits
1928907f744SAlan Somersbits referenced by
1938907f744SAlan Somers.Fa name .
1948907f744SAlan SomersIf no bits are set, the location referenced by
1958907f744SAlan Somers.Fa value
1968907f744SAlan Somersis set to \-1.
1978907f744SAlan Somers.Pp
1988907f744SAlan SomersThe function
1998907f744SAlan Somers.Fn bit_ffc_at
2008907f744SAlan Somersstores in the location referenced by
2018907f744SAlan Somers.Fa value
2028907f744SAlan Somersthe zero-based number of the first bit not set in the array of
2038907f744SAlan Somers.Fa nbits
2048907f744SAlan Somersbits referenced by
2058907f744SAlan Somers.Fa name ,
2068907f744SAlan Somersat or after the zero-based bit index
2078907f744SAlan Somers.Fa start .
2088907f744SAlan SomersIf all bits at or after
2098907f744SAlan Somers.Fa start
2108907f744SAlan Somersare set, the location referenced by
2118907f744SAlan Somers.Fa value
2128907f744SAlan Somersis set to \-1.
2138907f744SAlan Somers.Pp
2148907f744SAlan SomersThe
2158907f744SAlan Somers.Fn bit_ffs_at
2168907f744SAlan Somersfunction
2178907f744SAlan Somersstores in the location referenced by
2188907f744SAlan Somers.Fa value
2198907f744SAlan Somersthe zero-based number of the first bit set in the array of
2208907f744SAlan Somers.Fa nbits
2218907f744SAlan Somersbits referenced by
2228907f744SAlan Somers.Fa name ,
2238907f744SAlan Somersat or after the zero-based bit index
2248907f744SAlan Somers.Fa start .
2258907f744SAlan SomersIf no bits are set after
2268907f744SAlan Somers.Fa start ,
2278907f744SAlan Somersthe location referenced by
2288907f744SAlan Somers.Fa value
2298907f744SAlan Somersis set to \-1.
2308907f744SAlan Somers.Pp
231*1b82e02fSAlan SomersThe
232*1b82e02fSAlan Somers.Fn bit_count
233*1b82e02fSAlan Somersfunction stores in the location referenced by
234*1b82e02fSAlan Somers.Fa value
235*1b82e02fSAlan Somersthe number of bits set in the array of
236*1b82e02fSAlan Somers.Fa nbits
237*1b82e02fSAlan Somersbits referenced by
238*1b82e02fSAlan Somers.Fa name ,
239*1b82e02fSAlan Somersat or after the zero-based bit index
240*1b82e02fSAlan Somers.Fa start .
241*1b82e02fSAlan Somers.Pp
2428907f744SAlan SomersThe arguments in bit string macros are evaluated only once and may safely
243afe61c15SRodney W. Grimeshave side effects.
244251c176fSRuslan Ermilov.Sh EXAMPLES
245afe61c15SRodney W. Grimes.Bd -literal -offset indent
246afe61c15SRodney W. Grimes#include <limits.h>
247afe61c15SRodney W. Grimes#include <bitstring.h>
248afe61c15SRodney W. Grimes
2493136363fSRuslan Ermilov\&...
250afe61c15SRodney W. Grimes#define	LPR_BUSY_BIT		0
251afe61c15SRodney W. Grimes#define	LPR_FORMAT_BIT		1
252afe61c15SRodney W. Grimes#define	LPR_DOWNLOAD_BIT	2
2533136363fSRuslan Ermilov\&...
254afe61c15SRodney W. Grimes#define	LPR_AVAILABLE_BIT	9
255afe61c15SRodney W. Grimes#define	LPR_MAX_BITS		10
256afe61c15SRodney W. Grimes
257afe61c15SRodney W. Grimesmake_lpr_available()
258afe61c15SRodney W. Grimes{
259afe61c15SRodney W. Grimes	bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
260afe61c15SRodney W. Grimes	...
261afe61c15SRodney W. Grimes	bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
262afe61c15SRodney W. Grimes	...
263afe61c15SRodney W. Grimes	if (!bit_test(bitlist, LPR_BUSY_BIT)) {
264afe61c15SRodney W. Grimes		bit_clear(bitlist, LPR_FORMAT_BIT);
265afe61c15SRodney W. Grimes		bit_clear(bitlist, LPR_DOWNLOAD_BIT);
266afe61c15SRodney W. Grimes		bit_set(bitlist, LPR_AVAILABLE_BIT);
267afe61c15SRodney W. Grimes	}
268afe61c15SRodney W. Grimes}
269afe61c15SRodney W. Grimes.Ed
270afe61c15SRodney W. Grimes.Sh SEE ALSO
2717ebf4122SConrad Meyer.Xr malloc 3 ,
2727ebf4122SConrad Meyer.Xr bitset 9
273afe61c15SRodney W. Grimes.Sh HISTORY
274afe61c15SRodney W. GrimesThe
275afe61c15SRodney W. Grimes.Nm bitstring
27621421932SMike Pritchardfunctions first appeared in
27721421932SMike Pritchard.Bx 4.4 .
278