2 * Copyright 2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Brecht Machiels, brecht@mos6581.org
9 #ifndef _BINARY_UTILS_H
10 #define _BINARY_UTILS_H
13 /* macro for a one-bit bitmask
16 #define BIT(n) (1ULL << n)
18 /* macro/templates to create bitmasks
21 #define BITMASK(high, low) ((unsigned long long)(BitMask<high, low>::value))
23 template<int h
, int l
>
26 enum { value
= (BitMask
<h
-l
-1,0>::value
+
27 (1ULL << h
- l
)) << l
};
34 enum { value
= 0ULL };
38 /* macro/templates to enter binary constants
41 #define BINARY(binstring) ((unsigned long long)(Binary<0##binstring>::value))
43 // http://www.eptacom.net/pubblicazioni/pub_eng/binary.html
44 template<const unsigned long long N
> struct Binary
46 enum { value
= (N
% 8ULL) + 2ULL * Binary
<N
/ 8ULL>::value
};
53 enum { value
= 0ULL } ;
57 /* macro/templates to determine offset
59 NOTE: broken for high bit indices
62 #define MASKOFFSET(mask) (MaskOffset<mask, (mask & 1UL)>::count)
64 template<const unsigned long mask
, unsigned int firstBit
>
67 enum { count
= MaskOffset
<(mask
>> 1), ((mask
>> 1) & 1UL)>::count
+ 1 };
71 template<const unsigned long mask
>
72 struct MaskOffset
<mask
, 1>
77 #endif // _BINARY_UTILS_H