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