CARMA C++
BitMask.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_BITMASK_H
2 #define SZA_UTIL_BITMASK_H
3 
13 #include <valarray>
14 #include <iostream>
15 
16 namespace sza {
17  namespace util {
18 
19  class BitMask {
20  public:
21 
22  // Constructor.
23 
24  BitMask();
25 
26  // Destructor.
27 
28  virtual ~BitMask();
29 
30  // Resize this bit mask to be at least as large as the requested
31  // number of bits
32 
33  void resize(unsigned nBit);
34 
35  // Set up this object to manage an externally provided byte
36  // array
37 
38  void setTo(std::valarray<unsigned char>* byteVec);
39  void fillFrom(unsigned char* bytePtr, unsigned size);
40 
41  // Set the requested bit high
42 
43  void setBitHigh(unsigned iBit);
44  void setAllBitsHigh();
45 
46  // Set the requested bit low
47 
48  void setBitLow(unsigned iBit);
49  void setAllBitsLow();
50 
51  // Return if the requested bit is high or low
52 
53  bool bitIsHigh(unsigned iBit);
54  bool bitIsLow(unsigned iBit);
55 
56  // Return the requested bit value
57 
58  unsigned bitVal(unsigned iBit);
59 
60  // Return true if all bits are set low
61 
62  bool allBitsAreLow();
63  bool allBitsAreLowBut(unsigned iBit);
64 
65  // Print operator
66 
67  friend std::ostream& operator<<(std::ostream& os, BitMask& bitMask);
68 
69  // Assignment operators: bm1 = bm2
70 
71  void operator=(const BitMask& bitMask);
72  void operator=(BitMask& bitMask);
73 
74  // Operator for bm1 |= bm2, bm3 = bm1 | bm2
75 
76  void operator|=(BitMask& bitMask);
77  void operator|=(const BitMask& bitMask);
78  BitMask operator|(BitMask& bitMask);
79  BitMask operator|(const BitMask& bitMask);
80 
81  // Operator for bm1 &= bm2, bm3 = bm1 & bm2
82 
83  void operator&=(BitMask& bitMask);
84  void operator&=(const BitMask& bitMask);
85  BitMask operator&(BitMask& bitMask);
86  BitMask operator&(const BitMask& bitMask);
87 
88  // Operator for ~bm
89 
90  BitMask operator~ (void);
91 
92  void operator=(std::string bitMaskStr);
93 
94  public:
95 
96  void getBitAndByte(unsigned iBit);
97 
98  unsigned nBit_;
99  unsigned iByte_;
100  unsigned iBitInByte_;
101  unsigned byteVal_;
102  bool internalMemory_;
103 
104  std::valarray<unsigned char>* byteVecPtr_;
105 
106  }; // End class BitMask
107 
108  std::ostream& operator<<(std::ostream& os, BitMask& bitMask);
109 
110  } // End namespace util
111 } // End namespace sza
112 
113 
114 
115 #endif // End #ifndef SZA_UTIL_BITMASK_H