xref: /freebsd/share/man/man3/bitstring.3 (revision dda5b39711dab90ae1c5624bdd6ff7453177df31)
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.
14*dda5b397SEitan 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.\"
30afe61c15SRodney W. Grimes.\"     @(#)bitstring.3	8.1 (Berkeley) 7/19/93
317f3dea24SPeter Wemm.\" $FreeBSD$
32afe61c15SRodney W. Grimes.\"
33afe61c15SRodney W. Grimes.Dd July 19, 1993
34afe61c15SRodney W. Grimes.Dt BITSTRING 3
353d45e180SRuslan Ermilov.Os
36afe61c15SRodney W. Grimes.Sh NAME
37afe61c15SRodney W. Grimes.Nm bit_alloc ,
38afe61c15SRodney W. Grimes.Nm bit_clear ,
39afe61c15SRodney W. Grimes.Nm bit_decl ,
40afe61c15SRodney W. Grimes.Nm bit_ffs ,
41afe61c15SRodney W. Grimes.Nm bit_nclear ,
42afe61c15SRodney W. Grimes.Nm bit_nset ,
43afe61c15SRodney W. Grimes.Nm bit_set ,
44afe61c15SRodney W. Grimes.Nm bitstr_size ,
45afe61c15SRodney W. Grimes.Nm bit_test
46afe61c15SRodney W. Grimes.Nd bit-string manipulation macros
47afe61c15SRodney W. Grimes.Sh SYNOPSIS
4832eef9aeSRuslan Ermilov.In bitstring.h
49afe61c15SRodney W. Grimes.Ft bitstr_t *
50afe61c15SRodney W. Grimes.Fn bit_alloc "int nbits"
5169a6a1d7SBruce Evans.Ft void
5269a6a1d7SBruce Evans.Fn bit_decl "bitstr_t *name" "int nbits"
5369a6a1d7SBruce Evans.Ft void
5469a6a1d7SBruce Evans.Fn bit_clear "bitstr_t *name" "int bit"
5569a6a1d7SBruce Evans.Ft void
5669a6a1d7SBruce Evans.Fn bit_ffc "bitstr_t *name" "int nbits" "int *value"
5769a6a1d7SBruce Evans.Ft void
5869a6a1d7SBruce Evans.Fn bit_ffs "bitstr_t *name" "int nbits" "int *value"
5969a6a1d7SBruce Evans.Ft void
6069a6a1d7SBruce Evans.Fn bit_nclear "bitstr_t *name" "int start" "int stop"
6169a6a1d7SBruce Evans.Ft void
6269a6a1d7SBruce Evans.Fn bit_nset "bitstr_t *name" "int start" "int stop"
6369a6a1d7SBruce Evans.Ft void
6469a6a1d7SBruce Evans.Fn bit_set "bitstr_t *name" "int bit"
6569a6a1d7SBruce Evans.Ft int
66afe61c15SRodney W. Grimes.Fn bitstr_size "int nbits"
6769a6a1d7SBruce Evans.Ft int
6869a6a1d7SBruce Evans.Fn bit_test "bitstr_t *name" "int bit"
69afe61c15SRodney W. Grimes.Sh DESCRIPTION
70afe61c15SRodney W. GrimesThese macros operate on strings of bits.
71afe61c15SRodney W. Grimes.Pp
72afe61c15SRodney W. GrimesThe macro
73afe61c15SRodney W. Grimes.Fn bit_alloc
74afe61c15SRodney W. Grimesreturns a pointer of type
75afe61c15SRodney W. Grimes.Dq Fa "bitstr_t *"
76afe61c15SRodney W. Grimesto sufficient space to store
77afe61c15SRodney W. Grimes.Fa nbits
78afe61c15SRodney W. Grimesbits, or
79afe61c15SRodney W. Grimes.Dv NULL
80afe61c15SRodney W. Grimesif no space is available.
81afe61c15SRodney W. Grimes.Pp
82afe61c15SRodney W. GrimesThe macro
83afe61c15SRodney W. Grimes.Fn bit_decl
84afe61c15SRodney W. Grimesallocates sufficient space to store
85afe61c15SRodney W. Grimes.Fa nbits
86afe61c15SRodney W. Grimesbits on the stack.
87afe61c15SRodney W. Grimes.Pp
88afe61c15SRodney W. GrimesThe macro
89afe61c15SRodney W. Grimes.Fn bitstr_size
90afe61c15SRodney W. Grimesreturns the number of elements of type
91afe61c15SRodney W. Grimes.Fa bitstr_t
92afe61c15SRodney W. Grimesnecessary to store
93afe61c15SRodney W. Grimes.Fa nbits
94afe61c15SRodney W. Grimesbits.
95afe61c15SRodney W. GrimesThis is useful for copying bit strings.
96afe61c15SRodney W. Grimes.Pp
97afe61c15SRodney W. GrimesThe macros
98afe61c15SRodney W. Grimes.Fn bit_clear
99afe61c15SRodney W. Grimesand
100afe61c15SRodney W. Grimes.Fn bit_set
101afe61c15SRodney W. Grimesclear or set the zero-based numbered bit
102afe61c15SRodney W. Grimes.Fa bit ,
103afe61c15SRodney W. Grimesin the bit string
104afe61c15SRodney W. Grimes.Ar name .
105afe61c15SRodney W. Grimes.Pp
106afe61c15SRodney W. GrimesThe
107afe61c15SRodney W. Grimes.Fn bit_nset
108afe61c15SRodney W. Grimesand
109afe61c15SRodney W. Grimes.Fn bit_nclear
110afe61c15SRodney W. Grimesmacros
111afe61c15SRodney W. Grimesset or clear the zero-based numbered bits from
112afe61c15SRodney W. Grimes.Fa start
1130b607240SJohn-Mark Gurneythrough
114afe61c15SRodney W. Grimes.Fa stop
115afe61c15SRodney W. Grimesin the bit string
116afe61c15SRodney W. Grimes.Ar name .
117afe61c15SRodney W. Grimes.Pp
118afe61c15SRodney W. GrimesThe
119afe61c15SRodney W. Grimes.Fn bit_test
120afe61c15SRodney W. Grimesmacro
121afe61c15SRodney W. Grimesevaluates to non-zero if the zero-based numbered bit
122afe61c15SRodney W. Grimes.Fa bit
123afe61c15SRodney W. Grimesof bit string
124afe61c15SRodney W. Grimes.Fa name
125afe61c15SRodney W. Grimesis set, and zero otherwise.
126afe61c15SRodney W. Grimes.Pp
127afe61c15SRodney W. GrimesThe
128afe61c15SRodney W. Grimes.Fn bit_ffs
129afe61c15SRodney W. Grimesmacro
130afe61c15SRodney W. Grimesstores in the location referenced by
131afe61c15SRodney W. Grimes.Fa value
132afe61c15SRodney W. Grimesthe zero-based number of the first bit set in the array of
133afe61c15SRodney W. Grimes.Fa nbits
134afe61c15SRodney W. Grimesbits referenced by
135afe61c15SRodney W. Grimes.Fa name .
136afe61c15SRodney W. GrimesIf no bits are set, the location referenced by
137afe61c15SRodney W. Grimes.Fa value
138afe61c15SRodney W. Grimesis set to \-1.
139afe61c15SRodney W. Grimes.Pp
140afe61c15SRodney W. GrimesThe macro
141afe61c15SRodney W. Grimes.Fn bit_ffc
142afe61c15SRodney W. Grimesstores in the location referenced by
143afe61c15SRodney W. Grimes.Fa value
144afe61c15SRodney W. Grimesthe zero-based number of the first bit not set in the array of
145afe61c15SRodney W. Grimes.Fa nbits
146afe61c15SRodney W. Grimesbits referenced by
147afe61c15SRodney W. Grimes.Fa name .
148afe61c15SRodney W. GrimesIf all bits are set, the location referenced by
149afe61c15SRodney W. Grimes.Fa value
150afe61c15SRodney W. Grimesis set to \-1.
151afe61c15SRodney W. Grimes.Pp
152afe61c15SRodney W. GrimesThe arguments to these macros are evaluated only once and may safely
153afe61c15SRodney W. Grimeshave side effects.
154251c176fSRuslan Ermilov.Sh EXAMPLES
155afe61c15SRodney W. Grimes.Bd -literal -offset indent
156afe61c15SRodney W. Grimes#include <limits.h>
157afe61c15SRodney W. Grimes#include <bitstring.h>
158afe61c15SRodney W. Grimes
1593136363fSRuslan Ermilov\&...
160afe61c15SRodney W. Grimes#define	LPR_BUSY_BIT		0
161afe61c15SRodney W. Grimes#define	LPR_FORMAT_BIT		1
162afe61c15SRodney W. Grimes#define	LPR_DOWNLOAD_BIT	2
1633136363fSRuslan Ermilov\&...
164afe61c15SRodney W. Grimes#define	LPR_AVAILABLE_BIT	9
165afe61c15SRodney W. Grimes#define	LPR_MAX_BITS		10
166afe61c15SRodney W. Grimes
167afe61c15SRodney W. Grimesmake_lpr_available()
168afe61c15SRodney W. Grimes{
169afe61c15SRodney W. Grimes	bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
170afe61c15SRodney W. Grimes	...
171afe61c15SRodney W. Grimes	bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
172afe61c15SRodney W. Grimes	...
173afe61c15SRodney W. Grimes	if (!bit_test(bitlist, LPR_BUSY_BIT)) {
174afe61c15SRodney W. Grimes		bit_clear(bitlist, LPR_FORMAT_BIT);
175afe61c15SRodney W. Grimes		bit_clear(bitlist, LPR_DOWNLOAD_BIT);
176afe61c15SRodney W. Grimes		bit_set(bitlist, LPR_AVAILABLE_BIT);
177afe61c15SRodney W. Grimes	}
178afe61c15SRodney W. Grimes}
179afe61c15SRodney W. Grimes.Ed
180afe61c15SRodney W. Grimes.Sh SEE ALSO
181afe61c15SRodney W. Grimes.Xr malloc 3
182afe61c15SRodney W. Grimes.Sh HISTORY
183afe61c15SRodney W. GrimesThe
184afe61c15SRodney W. Grimes.Nm bitstring
18521421932SMike Pritchardfunctions first appeared in
18621421932SMike Pritchard.Bx 4.4 .
187