]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bits.h
ef88741808a6412c546166362e6c12cafc123bf9
[bacula/bacula] / bacula / src / lib / bits.h
1 /* Some elementary bit manipulations
2  *
3  *   Kern Sibbald, MM
4  *
5  *  NOTE:  base 0
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #ifndef __BITS_H_
28 #define __BITS_H_
29
30 /* number of bytes to hold n bits */
31 #define nbytes_for_bits(n) ((((n)-1)>>3)+1) 
32
33 /* test if bit is set */
34 #define bit_is_set(b, var) ((var)[(b)>>3] & (1<<((b)&0x7)))
35
36 /* set bit */
37 #define set_bit(b, var) ((var)[(b)>>3] |= (1<<((b)&0x7)))
38
39 /* clear bit */
40 #define clear_bit(b, var) ((var)[(b)>>3] &= ~(1<<((b)&0x7)))
41
42 /* clear all bits */
43 #define clear_all_bits(b, var) memset(var, 0, nbytes_for_bits(b))
44
45 /* set range of bits */
46 #define set_bits(f, l, var) { \
47    int i; \
48    for (i=f; i<=l; i++)  \
49       set_bit(i, var); \
50 }
51  
52 /* clear range of bits */
53 #define clear_bits(f, l, var) { \
54    int i; \
55    for (i=f; i<=l; i++)  \
56       clear_bit(i, var); \
57 }
58
59 #endif /* __BITS_H_ */