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