ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataTree/interface/BitMask.h
Revision: 1.4
Committed: Wed Sep 17 05:26:23 2008 UTC (16 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.3: +2 -2 lines
Log Message:
BugFix in assert logic.

File Contents

# User Rev Content
1 loizides 1.3 //--------------------------------------------------------------------------------------------------
2 loizides 1.4 // $Id: BitMask.h,v 1.3 2008/09/17 04:09:26 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     BitMask(const Char_t *bits) { SetBits(bits); }
25     virtual ~BitMask() {}
26    
27     const Char_t *Bits() const { return fBitMask; }
28     void Clear() { memset(fBitMask,0,N); }
29     void ClearBit(UInt_t n) { SetBit(n,0); }
30     UInt_t NBitsSet(UInt_t first=0, UInt_t last=N*8) const;
31     void SetBit(UInt_t n, Bool_t b=1);
32     void SetBits(const Char_t *bits) { strncpy(fBitMask,bits,N); }
33     void SetBits(Long64_t bits);
34     UInt_t Size() const { return N*8; }
35     Bool_t TestBit(UInt_t n) const;
36    
37     protected:
38     Char_t fBitMask[N]; //the actual bitmask
39    
40     ClassDefT(BitMask, 1)
41     };
42     }
43    
44     //--------------------------------------------------------------------------------------------------
45     template<UInt_t N>
46     inline void mithep::BitMask<N>::SetBit(UInt_t n, Bool_t b)
47     {
48     // Set nth bit to given value.
49    
50     if(n>=N*8) return;
51    
52     UInt_t loc = n/8;
53     UChar_t bit = n%8;
54     if (b)
55     fBitMask[loc] |= (1<<bit);
56     else
57     fBitMask[loc] &= (0xFF ^ (1<<bit));
58     }
59    
60     //--------------------------------------------------------------------------------------------------
61     template<UInt_t N>
62     UInt_t mithep::BitMask<N>::NBitsSet(UInt_t first, UInt_t last) const
63     {
64     // Count number of bits which are set.
65    
66     UInt_t numBits = 0;
67     for (UInt_t i=first; i<=last; i++)
68     numBits += TestBit(i);
69    
70     return numBits;
71     }
72    
73     //--------------------------------------------------------------------------------------------------
74     template<UInt_t N>
75     inline void mithep::BitMask<N>::SetBits(Long64_t bits)
76     {
77     // Set bits given by bits.
78    
79 loizides 1.4 assert(sizeof(Long64_t)>=N);
80 loizides 1.3 SetBits(reinterpret_cast<const char*>(&bits));
81     }
82    
83     //--------------------------------------------------------------------------------------------------
84     template<UInt_t N>
85     inline Bool_t mithep::BitMask<N>::TestBit(UInt_t n) const
86     {
87     // Return true if nth bit is set.
88    
89     if(n>=N*8) return 0;
90    
91     UInt_t loc = n/8;
92     UChar_t bit = n%8;
93     Char_t val = fBitMask[loc];
94     Bool_t result = (val & (1<<bit)) != 0;
95     return result;
96     }
97     #endif