1*48349Scael.\" Copyright (c) 1989, 1991 The Regents of the University of California. 239706Sbostic.\" All rights reserved. 339706Sbostic.\" 439706Sbostic.\" This code is derived from software contributed to Berkeley by 539706Sbostic.\" Paul Vixie. 643583Strent.\" %sccs.include.redist.man% 739706Sbostic.\" 8*48349Scael.\" @(#)bitstring.3 5.4 (Berkeley) 04/19/91 939706Sbostic.\" 10*48349Scael.Dd 11*48349Scael.Dt BITSTRING 3 12*48349Scael.Os BSD 4 13*48349Scael.Sh NAME 14*48349Scael.Nm bit_alloc , 15*48349Scael.Nm bit_clear , 16*48349Scael.Nm bit_decl , 17*48349Scael.Nm bit_ffs , 18*48349Scael.Nm bit_nclear , 19*48349Scael.Nm bit_nset, 20*48349Scael.Nm bit_set , 21*48349Scael.Nm bitstr_size , 22*48349Scael.Nm bit_test 23*48349Scael.Nd bit-string manipulation macros 24*48349Scael.Sh SYNOPSIS 25*48349Scael.Fd #include <bitstring.h> 26*48349Scael.Ft bitstr_t * 27*48349Scael.Fn bit_alloc "int nbits" 28*48349Scael.Fn bit_decl "bit_str name" "int nbits" 29*48349Scael.Fn bit_clear "bit_str name" "int bit" 30*48349Scael.Fn bit_ffc "bit_str name" "int nbits" "int *value" 31*48349Scael.Fn bit_ffs "bit_str name" "int nbits" "int *value" 32*48349Scael.Fn bit_nclear "bit_str name" "int start" "int stop" 33*48349Scael.Fn bit_nset "bit_str name" "int start" "int stop" 34*48349Scael.Fn bit_set "bit_str name" "int bit" 35*48349Scael.Fn bitstr_size "int nbits" 36*48349Scael.Fn bit_test "bit_str name" "int bit" 37*48349Scael.Sh DESCRIPTION 3839706SbosticThese macros operate on strings of bits. 39*48349Scael.Pp 40*48349ScaelThe macro 41*48349Scael.Fn bit_alloc 4239706Sbosticreturns a pointer of type 43*48349Scael.Dq Fa "bitstr_t *" 4439706Sbosticto sufficient space to store 45*48349Scael.Fa nbits 46*48349Scaelbits, or 47*48349Scael.Dv NULL 48*48349Scaelif no space is available. 49*48349Scael.Pp 50*48349ScaelThe macro 51*48349Scael.Fn bit_decl 52*48349Scaelallocates sufficient space to store 53*48349Scael.Fa nbits 5439706Sbosticbits on the stack. 55*48349Scael.Pp 56*48349ScaelThe macro 57*48349Scael.Fn bitstr_size 5839706Sbosticreturns the number of elements of type 59*48349Scael.Fa bitstr_t 6039706Sbosticnecessary to store 61*48349Scael.Fa nbits 6239706Sbosticbits. 6339706SbosticThis is useful for copying bit strings. 64*48349Scael.Pp 65*48349ScaelThe macros 66*48349Scael.Fn bit_clear 6739706Sbosticand 68*48349Scael.Fn bit_set 6939706Sbosticclear or set the zero-based numbered bit 70*48349Scael.Fa bit , 7139706Sbosticin the bit string 72*48349Scael.Ar name . 73*48349Scael.Pp 74*48349ScaelThe 75*48349Scael.Fn bit_nset 7639706Sbosticand 77*48349Scael.Fn bit_nclear 78*48349Scaelmacros 7939706Sbosticset or clear the zero-based numbered bits from 80*48349Scael.Fa start 8139706Sbosticto 82*48349Scael.Fa stop 8339706Sbosticin the bit string 84*48349Scael.Ar name . 85*48349Scael.Pp 86*48349ScaelThe 87*48349Scael.Fn bit_test 88*48349Scaelmacro 8939706Sbosticevaluates to zero if the zero-based numbered bit 90*48349Scael.Fa bit 9139706Sbosticof bit string 92*48349Scael.Fa name 9339706Sbosticis set, and non-zero otherwise. 94*48349Scael.Pp 95*48349ScaelThe 96*48349Scael.Fn bit_ffs 97*48349Scaelmacro 9840741Sbosticstores in the location referenced by 99*48349Scael.Fa value 10039706Sbosticthe zero-based number of the first bit set in the array of 101*48349Scael.Fa nbits 10239706Sbosticbits referenced by 103*48349Scael.Fa name . 10440741SbosticIf no bits are set, the location referenced by 105*48349Scael.Fa value 106*48349Scaelis set to \-1. 107*48349Scael.Pp 108*48349ScaelThe macro 109*48349Scael.Fn bit_ffc 11040741Sbosticstores in the location referenced by 111*48349Scael.Fa value 11239706Sbosticthe zero-based number of the first bit not set in the array of 113*48349Scael.Fa nbits 11439706Sbosticbits referenced by 115*48349Scael.Fa name . 11640741SbosticIf all bits are set, the location referenced by 117*48349Scael.Fa value 118*48349Scaelis set to \-1. 119*48349Scael.Pp 12040741SbosticThe arguments to these macros are evaluated only once and may safely 12140741Sbostichave side effects. 122*48349Scael.Sh EXAMPLE 123*48349Scael.Bd -literal -offset indent 12439706Sbostic#include <limits.h> 12539706Sbostic#include <bitstring.h> 12639706Sbostic 12739706Sbostic... 12839706Sbostic#define LPR_BUSY_BIT 0 12939706Sbostic#define LPR_FORMAT_BIT 1 13039706Sbostic#define LPR_DOWNLOAD_BIT 2 13139706Sbostic... 13239706Sbostic#define LPR_AVAILABLE_BIT 9 13339706Sbostic#define LPR_MAX_BITS 10 13439706Sbostic 13539706Sbosticmake_lpr_available() 13639706Sbostic{ 13739706Sbostic bitstr_t bit_decl(bitlist, LPR_MAX_BITS); 13839706Sbostic ... 13939706Sbostic bit_nclear(bitlist, 0, LPR_MAX_BITS - 1); 14039706Sbostic ... 14139706Sbostic if (!bit_test(bitlist, LPR_BUSY_BIT)) { 14239706Sbostic bit_clear(bitlist, LPR_FORMAT_BIT); 14339706Sbostic bit_clear(bitlist, LPR_DOWNLOAD_BIT); 14439706Sbostic bit_set(bitlist, LPR_AVAILABLE_BIT); 14539706Sbostic } 14639706Sbostic} 147*48349Scael.Ed 148*48349Scael.Sh SEE ALSO 149*48349Scael.Xr malloc 3 150*48349Scael.Sh HISTORY 151*48349ScaelThe 152*48349Scael.Nm 153*48349Scaelfunctions are 154*48349Scael.Ud . 155