1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
5    Copyright (C) 2014-2016 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, MM
24  */
25 /*
26  * Some elementary bit manipulations
27  * NOTE:  base 0
28  */
29 
30 #ifndef BAREOS_LIB_BITS_H_
31 #define BAREOS_LIB_BITS_H_
32 
33 /*
34  * Number of bytes to hold n bits
35  */
36 #define NbytesForBits(n) ((((n) - 1) >> 3) + 1)
37 
38 /*
39  * Test if bit is set
40  */
41 #define BitIsSet(b, var) (((var)[(b) >> 3] & (1 << ((b) & 0x7))) != 0)
42 
43 /*
44  * Set bit
45  */
46 #define SetBit(b, var) ((var)[(b) >> 3] |= (1 << ((b) & 0x7)))
47 
48 /*
49  * Clear bit
50  */
51 #define ClearBit(b, var) ((var)[(b) >> 3] &= ~(1 << ((b) & 0x7)))
52 
53 /*
54  * Clear all bits
55  */
56 #define ClearAllBits(b, var) memset((var), 0, NbytesForBits((b)))
57 
58 /*
59  * Set range of bits
60  */
61 #define SetBits(f, l, var) { \
62    int bit; \
63    for (bit = (f); bit <= (l); bit++)  \
64       SetBit(bit, (var)); \
65 }
66 
67 /*
68  * Clear range of bits
69  */
70 #define ClearBits(f, l, var) { \
71    int bit; \
72    for (bit = (f); bit <= (l); bit++)  \
73       ClearBit(bit, (var)); \
74 }
75 
76 /*
77  * Clone all set bits from var1 to var2
78  */
79 #define CopySetBits(l, var1, var2) { \
80    int bit; \
81    for (bit = 0; bit <= (l); bit++)  \
82       if (BitIsSet(bit, (var1))) \
83          SetBit(bit, (var2)); \
84 }
85 
86 /*
87  * Copy all bits from var1 to var2
88  */
89 #define CopyBits(b, var1, var2) memcpy((var2), (var1), NbytesForBits((b)))
90 
91 #endif /* BAREOS_LIB_BITS_H_ */
92