1*63898Sbostic.\" Copyright (c) 1989, 1991, 1993 2*63898Sbostic.\" The Regents of the University of California. 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*63898Sbostic.\" @(#)bitstring.3 8.1 (Berkeley) 07/19/93 939706Sbostic.\" 1048349Scael.Dd 1148349Scael.Dt BITSTRING 3 1248349Scael.Os BSD 4 1348349Scael.Sh NAME 1448349Scael.Nm bit_alloc , 1548349Scael.Nm bit_clear , 1648349Scael.Nm bit_decl , 1748349Scael.Nm bit_ffs , 1848349Scael.Nm bit_nclear , 1948349Scael.Nm bit_nset, 2048349Scael.Nm bit_set , 2148349Scael.Nm bitstr_size , 2248349Scael.Nm bit_test 2348349Scael.Nd bit-string manipulation macros 2448349Scael.Sh SYNOPSIS 2548349Scael.Fd #include <bitstring.h> 2648349Scael.Ft bitstr_t * 2748349Scael.Fn bit_alloc "int nbits" 2848349Scael.Fn bit_decl "bit_str name" "int nbits" 2948349Scael.Fn bit_clear "bit_str name" "int bit" 3048349Scael.Fn bit_ffc "bit_str name" "int nbits" "int *value" 3148349Scael.Fn bit_ffs "bit_str name" "int nbits" "int *value" 3248349Scael.Fn bit_nclear "bit_str name" "int start" "int stop" 3348349Scael.Fn bit_nset "bit_str name" "int start" "int stop" 3448349Scael.Fn bit_set "bit_str name" "int bit" 3548349Scael.Fn bitstr_size "int nbits" 3648349Scael.Fn bit_test "bit_str name" "int bit" 3748349Scael.Sh DESCRIPTION 3839706SbosticThese macros operate on strings of bits. 3948349Scael.Pp 4048349ScaelThe macro 4148349Scael.Fn bit_alloc 4239706Sbosticreturns a pointer of type 4348349Scael.Dq Fa "bitstr_t *" 4439706Sbosticto sufficient space to store 4548349Scael.Fa nbits 4648349Scaelbits, or 4748349Scael.Dv NULL 4848349Scaelif no space is available. 4948349Scael.Pp 5048349ScaelThe macro 5148349Scael.Fn bit_decl 5248349Scaelallocates sufficient space to store 5348349Scael.Fa nbits 5439706Sbosticbits on the stack. 5548349Scael.Pp 5648349ScaelThe macro 5748349Scael.Fn bitstr_size 5839706Sbosticreturns the number of elements of type 5948349Scael.Fa bitstr_t 6039706Sbosticnecessary to store 6148349Scael.Fa nbits 6239706Sbosticbits. 6339706SbosticThis is useful for copying bit strings. 6448349Scael.Pp 6548349ScaelThe macros 6648349Scael.Fn bit_clear 6739706Sbosticand 6848349Scael.Fn bit_set 6939706Sbosticclear or set the zero-based numbered bit 7048349Scael.Fa bit , 7139706Sbosticin the bit string 7248349Scael.Ar name . 7348349Scael.Pp 7448349ScaelThe 7548349Scael.Fn bit_nset 7639706Sbosticand 7748349Scael.Fn bit_nclear 7848349Scaelmacros 7939706Sbosticset or clear the zero-based numbered bits from 8048349Scael.Fa start 8139706Sbosticto 8248349Scael.Fa stop 8339706Sbosticin the bit string 8448349Scael.Ar name . 8548349Scael.Pp 8648349ScaelThe 8748349Scael.Fn bit_test 8848349Scaelmacro 8963897Sbosticevaluates to non-zero if the zero-based numbered bit 9048349Scael.Fa bit 9139706Sbosticof bit string 9248349Scael.Fa name 9363897Sbosticis set, and zero otherwise. 9448349Scael.Pp 9548349ScaelThe 9648349Scael.Fn bit_ffs 9748349Scaelmacro 9840741Sbosticstores in the location referenced by 9948349Scael.Fa value 10039706Sbosticthe zero-based number of the first bit set in the array of 10148349Scael.Fa nbits 10239706Sbosticbits referenced by 10348349Scael.Fa name . 10440741SbosticIf no bits are set, the location referenced by 10548349Scael.Fa value 10648349Scaelis set to \-1. 10748349Scael.Pp 10848349ScaelThe macro 10948349Scael.Fn bit_ffc 11040741Sbosticstores in the location referenced by 11148349Scael.Fa value 11239706Sbosticthe zero-based number of the first bit not set in the array of 11348349Scael.Fa nbits 11439706Sbosticbits referenced by 11548349Scael.Fa name . 11640741SbosticIf all bits are set, the location referenced by 11748349Scael.Fa value 11848349Scaelis set to \-1. 11948349Scael.Pp 12040741SbosticThe arguments to these macros are evaluated only once and may safely 12140741Sbostichave side effects. 12248349Scael.Sh EXAMPLE 12348349Scael.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} 14748349Scael.Ed 14848349Scael.Sh SEE ALSO 14948349Scael.Xr malloc 3 15048349Scael.Sh HISTORY 15148349ScaelThe 15262960Sbostic.Nm bitstring 15362960Sbosticfunctions first appeared in 4.4BSD. 154