]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bits.h
Backport from BEE
[bacula/bacula] / bacula / src / lib / bits.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /* Some elementary bit manipulations
17  *
18  *   Kern Sibbald, MM
19  *
20  *  NOTE:  base 0
21  *
22  */
23
24 #ifndef __BITS_H_
25 #define __BITS_H_
26
27 /* number of bytes to hold n bits */
28 #define nbytes_for_bits(n) ((((n)-1)>>3)+1)
29
30 /* test if bit is set */
31 #define bit_is_set(b, var) (((var)[(b)>>3] & (1<<((b)&0x7))) != 0)
32
33 /* set bit */
34 #define set_bit(b, var) ((var)[(b)>>3] |= (1<<((b)&0x7)))
35
36 /* clear bit */
37 #define clear_bit(b, var) ((var)[(b)>>3] &= ~(1<<((b)&0x7)))
38
39 /* clear all bits */
40 #define clear_all_bits(b, var) memset(var, 0, nbytes_for_bits(b))
41
42 /* set range of bits */
43 #define set_bits(f, l, var) { \
44    int i; \
45    for (i=f; i<=l; i++)  \
46       set_bit(i, var); \
47 }
48
49 /* clear range of bits */
50 #define clear_bits(f, l, var) { \
51    int i; \
52    for (i=f; i<=l; i++)  \
53       clear_bit(i, var); \
54 }
55
56 #endif /* __BITS_H_ */