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