ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataTree/interface/BitMask.h
Revision: 1.6
Committed: Mon Oct 13 10:34:30 2008 UTC (16 years, 6 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.5: +25 -6 lines
Log Message:
Added bitwise operators to bitmask

File Contents

# User Rev Content
1 loizides 1.3 //--------------------------------------------------------------------------------------------------
2 bendavid 1.6 // $Id: BitMask.h,v 1.5 2008/09/27 06:09:27 loizides Exp $
3 loizides 1.3 //
4     // BitMask
5     //
6     // Helper class implementing a Nx8 bit bitmask.
7     //
8     // Authors: J.Bendavid, C.Loizides
9     //--------------------------------------------------------------------------------------------------
10    
11     #ifndef MITANA_DATATREE_BITMASK_H
12     #define MITANA_DATATREE_BITMASK_H
13    
14     #include <assert.h>
15     #include <Rtypes.h>
16    
17     namespace mithep
18     {
19     template<UInt_t N>
20     class BitMask
21     {
22     public:
23     BitMask() { Clear(); }
24 loizides 1.5 BitMask(const char *bits) { SetBits(bits); }
25     BitMask(const BitMask<N> &copy) { SetBits(copy.Bits()); }
26 loizides 1.3 virtual ~BitMask() {}
27    
28 loizides 1.5 const char *Bits() const { return fBitMask; }
29 loizides 1.3 void Clear() { memset(fBitMask,0,N); }
30     void ClearBit(UInt_t n) { SetBit(n,0); }
31     UInt_t NBitsSet(UInt_t first=0, UInt_t last=N*8) const;
32     void SetBit(UInt_t n, Bool_t b=1);
33 loizides 1.5 void SetBits(const Char_t *bits) { memcpy(fBitMask,bits,N); }
34 loizides 1.3 void SetBits(Long64_t bits);
35     UInt_t Size() const { return N*8; }
36     Bool_t TestBit(UInt_t n) const;
37 loizides 1.5 BitMask &operator&=(const BitMask<N> &rhs)
38     { for (UInt_t n=0; n<N; ++n) fBitMask[n]&=rhs.fBitMask[n]; return *this; }
39     BitMask &operator|=(const BitMask<N> &rhs)
40     { for (UInt_t n=0; n<N; ++n) fBitMask[n]|=rhs.fBitMask[n]; return *this; }
41     Bool_t operator!=(const BitMask<N> &other) const;
42     Bool_t operator==(const BitMask<N> &other) const;
43 bendavid 1.6 BitMask operator& (const BitMask &other) const
44     { return BitMask<N>(*this) &= other; }
45     BitMask operator| (const BitMask &other) const
46     { return BitMask<N>(*this) |= other; }
47     BitMask operator ~ () const;
48    
49    
50 loizides 1.3 protected:
51     Char_t fBitMask[N]; //the actual bitmask
52    
53     ClassDefT(BitMask, 1)
54     };
55     }
56    
57     //--------------------------------------------------------------------------------------------------
58     template<UInt_t N>
59     inline void mithep::BitMask<N>::SetBit(UInt_t n, Bool_t b)
60     {
61     // Set nth bit to given value.
62    
63     if(n>=N*8) return;
64    
65     UInt_t loc = n/8;
66     UChar_t bit = n%8;
67     if (b)
68     fBitMask[loc] |= (1<<bit);
69     else
70     fBitMask[loc] &= (0xFF ^ (1<<bit));
71     }
72    
73     //--------------------------------------------------------------------------------------------------
74     template<UInt_t N>
75     UInt_t mithep::BitMask<N>::NBitsSet(UInt_t first, UInt_t last) const
76     {
77     // Count number of bits which are set.
78    
79     UInt_t numBits = 0;
80     for (UInt_t i=first; i<=last; i++)
81     numBits += TestBit(i);
82    
83     return numBits;
84     }
85    
86     //--------------------------------------------------------------------------------------------------
87     template<UInt_t N>
88     inline void mithep::BitMask<N>::SetBits(Long64_t bits)
89     {
90     // Set bits given by bits.
91    
92 loizides 1.4 assert(sizeof(Long64_t)>=N);
93 loizides 1.3 SetBits(reinterpret_cast<const char*>(&bits));
94     }
95    
96     //--------------------------------------------------------------------------------------------------
97     template<UInt_t N>
98     inline Bool_t mithep::BitMask<N>::TestBit(UInt_t n) const
99     {
100     // Return true if nth bit is set.
101    
102     if(n>=N*8) return 0;
103    
104     UInt_t loc = n/8;
105     UChar_t bit = n%8;
106     Char_t val = fBitMask[loc];
107     Bool_t result = (val & (1<<bit)) != 0;
108     return result;
109     }
110 loizides 1.5
111     //--------------------------------------------------------------------------------------------------
112     template<UInt_t N>
113     Bool_t mithep::BitMask<N>::operator==(const mithep::BitMask<N> &other) const
114     {
115     // Equal operator.
116    
117     for (UInt_t n=0; n<N; ++n) {
118     if (fBitMask[n]!=other.fBitMask[n])
119     return kFALSE;
120     }
121     return kTRUE;
122     }
123    
124     //--------------------------------------------------------------------------------------------------
125     template<UInt_t N>
126     Bool_t mithep::BitMask<N>::operator!=(const mithep::BitMask<N> &other) const
127     {
128     // Unequal operator.
129    
130     for (UInt_t n=0; n<N; ++n) {
131 bendavid 1.6 if (fBitMask[n]!=other.fBitMask[n])
132     return kTRUE;
133     }
134     return kFALSE;
135     }
136    
137     //--------------------------------------------------------------------------------------------------
138     template<UInt_t N>
139     mithep::BitMask<N> mithep::BitMask<N>::operator ~() const
140     {
141     // bitwise inversion operator
142     BitMask<N> bits;
143     for (UInt_t n=0; n<N; ++n) {
144     bits.fBitMask[n] = ~fBitMask[n];
145 loizides 1.5 }
146 bendavid 1.6
147     return bits;
148     }
149 loizides 1.3 #endif