nidas  v1.2-1520
BitArray.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_UTIL_BITARRAY_H
28 #define NIDAS_UTIL_BITARRAY_H
29 
30 #include <string>
31 
32 namespace nidas { namespace util {
33 
39 class BitArray {
40 
41 protected:
42 
49  unsigned char *bits;
50  int lenBits;
51  int lenBytes;
52 
53 public:
54 
58  BitArray(int lenbits);
59 
63  BitArray(const BitArray& ba);
64 
68  BitArray& operator = (const BitArray& ba);
69 
70  ~BitArray();
71 
75  void setBits(bool value);
76 
81  void setBit(int num, bool value)
82  {
83  if (num >= getLengthInBytes() * 8) return;
84  if (value) bits[num/8] |= 0x1 << (num%8);
85  else bits[num/8] &= ~(0x1 << (num%8));
86  }
87 
99  void setBits(int begin,int end, unsigned int bitmask);
100 
101  void setBits64(int begin,int end, unsigned long long bitmask);
102 
108  bool getBit(int num) const
109  {
110  if (num >= getLength()) return false;
111  return (bits[num/8] & (0x1 << (num%8))) != 0;
112  }
113 
118  unsigned int getBits(int begin, int end);
119 
124  long long getBits64(int begin, int end);
125 
130  unsigned char* getPtr() { return bits; }
131 
136  const unsigned char* getConstPtr() const { return bits; }
137 
138  bool any() const
139  {
140  for (int i = 0; i < getLength(); i++)
141  if (getBit(i) != 0) return true;
142  return false;
143  }
144 
145  bool all() const
146  {
147  for (int i = 0; i < getLength(); i++)
148  if (getBit(i) == 0) return false;
149  return true;
150  }
151 
152  bool any(int begin,int end) const
153  {
154  for (int i = begin; i < std::min(end,getLength()); i++)
155  if (getBit(i) != 0) return true;
156  return false;
157  }
158 
159  bool all(int begin,int end) const
160  {
161  for (int i = begin; i < std::min(end,getLength()); i++)
162  if (getBit(i) == 0) return false;
163  return true;
164  }
165 
169  int getLength() const { return lenBits; }
170 
174  int getLengthInBytes() const { return lenBytes; }
175 
176  std::string toString() const;
177 
178  BitArray& operator |= (const BitArray& x);
179 
180  BitArray operator | (const BitArray& x);
181 
182  BitArray& operator &= (const BitArray& x);
183 
184  BitArray operator & (const BitArray& x);
185 
186  BitArray& operator ^= (const BitArray& x);
187 
188  BitArray operator ^ (const BitArray& x);
189 
190 };
191 
192 BitArray operator | (const BitArray& x,const BitArray& y);
193 
194 BitArray operator & (const BitArray& x,const BitArray& y);
195 
196 BitArray operator ^ (const BitArray& x,const BitArray& y);
197 
198 bool operator == (const BitArray& x,const BitArray& y);
199 
200 bool operator != (const BitArray& x,const BitArray& y);
201 
202 } } // namespace nidas namespace util
203 
204 #endif
unsigned char * getPtr()
Return pointer to first byte of BitArray.
Definition: BitArray.h:130
BitArray(int lenbits)
Constructor, all bits will be initialized to 0.
Definition: BitArray.cc:37
BitArray operator|(const BitArray &x, const BitArray &y)
Definition: BitArray.cc:202
void setBit(int num, bool value)
Set a bit, in range 0:(length()-1) to 1 if value is true, otherwise false.
Definition: BitArray.h:81
A class for holding bits.
Definition: BitArray.h:39
BitArray operator&(const BitArray &x)
Definition: BitArray.cc:291
int lenBits
Definition: BitArray.h:50
BitArray operator^(const BitArray &x)
Definition: BitArray.cc:357
bool any(int begin, int end) const
Definition: BitArray.h:152
std::string toString() const
Definition: BitArray.cc:164
BitArray operator^(const BitArray &x, const BitArray &y)
Definition: BitArray.cc:333
int lenBytes
Definition: BitArray.h:51
BitArray & operator=(const BitArray &ba)
Assignment operator.
Definition: BitArray.cc:59
BitArray & operator^=(const BitArray &x)
Definition: BitArray.cc:379
bool operator!=(const BitArray &x, const BitArray &y)
Definition: BitArray.cc:197
BitArray & operator|=(const BitArray &x)
Definition: BitArray.cc:248
BitArray operator|(const BitArray &x)
Definition: BitArray.cc:225
void setBits64(int begin, int end, unsigned long long bitmask)
Definition: BitArray.cc:98
bool operator==(const BitArray &x, const BitArray &y)
Definition: BitArray.cc:176
const unsigned char * getConstPtr() const
Return const pointer to first byte of BitArray.
Definition: BitArray.h:136
~BitArray()
Definition: BitArray.cc:51
long long getBits64(int begin, int end)
Return a long long of bits.
Definition: BitArray.cc:137
unsigned char * bits
To make things portable across different machine endiannesses we represent the bits in an array of ch...
Definition: BitArray.h:49
BitArray operator&(const BitArray &x, const BitArray &y)
Definition: BitArray.cc:268
void setBits(bool value)
Set all bits to 1 if value is true, otherwise set all to 0.
Definition: BitArray.cc:75
bool all(int begin, int end) const
Definition: BitArray.h:159
int getLengthInBytes() const
Length of array, in bytes.
Definition: BitArray.h:174
int getLength() const
Length of array, in bits.
Definition: BitArray.h:169
bool getBit(int num) const
Get value of a bit.
Definition: BitArray.h:108
bool any() const
Definition: BitArray.h:138
BitArray & operator&=(const BitArray &x)
Definition: BitArray.cc:313
unsigned int getBits(int begin, int end)
Return a integer of bits.
Definition: BitArray.cc:110
bool all() const
Definition: BitArray.h:145