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