TBTK
Need a break? Support the development by playing Polarity Puzzles
BitRegister.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_BIT_REGISTER
24 #define COM_DAFER45_TBTK_BIT_REGISTER
25 
26 #include "TBTK/Streams.h"
27 
28 #include <sstream>
29 
30 namespace TBTK{
31 
42 public:
44  BitRegister(unsigned int numBits = 8*sizeof(unsigned int));
45 
47  BitRegister(const BitRegister &bitRegister);
48 
50  const BitRegister operator|(const BitRegister &rhs) const;
51 
53  const BitRegister operator&(const BitRegister &rhs) const;
54 
56  const BitRegister operator^(const BitRegister &rhs) const;
57 
59  const BitRegister operator+(const BitRegister &rhs) const;
60 
62  const BitRegister operator-(const BitRegister &rhs) const;
63 
65  bool operator<(const BitRegister &rhs) const;
66 
68  bool operator>(const BitRegister &rhs) const;
69 
71  bool operator==(const BitRegister &rhs) const;
72 
74  void operator+=(const BitRegister &rhs);
75 
77  void operator-=(const BitRegister &rhs);
78 
80  const BitRegister& operator++();
81 
83  const BitRegister operator++(int);
84 
86  const BitRegister& operator--();
87 
89  const BitRegister operator--(int);
90 
92  void operator=(const BitRegister &rhs);
93 
95  void operator=(unsigned int rhs);
96 
98  BitRegister operator<<(unsigned int rhs) const;
99 
101  BitRegister operator>>(unsigned int rhs) const;
102 
104  void setBit(unsigned int position, bool values);
105 
107  bool getBit(unsigned int position) const;
108 
110  void setValues(unsigned int values);
111 
113  unsigned int getValues() const;
114 
117  bool toBool() const;
118 
120  unsigned int toUnsignedInt() const;
121 
123  void clear();
124 
126  void print() const;
127 
129  unsigned int getNumBits() const;
130 
132  unsigned int getNumOneBits() const;
133 
135  bool getMostSignificantBit() const;
136 
138  void setMostSignificantBit();
139 
142 
146  BitRegister cloneStructure() const;
147 
151  std::string toString() const;
152 
159  friend std::ostream& operator<<(
160  std::ostream &stream,
161  const BitRegister &bitRegister
162  );
163 
165 // unsigned int getAsUnsignedInt() const;
166 private:
168  unsigned int values;
169 
171  static constexpr unsigned int MOST_SIGNIFICANT_BIT_MASK
172  = (unsigned int)0x1 << (8*sizeof(unsigned int)-1);
173 };
174 
175 inline const BitRegister BitRegister::operator|(const BitRegister &rhs) const{
176  BitRegister result;
177  result.values = values | rhs.values;
178  return result;
179 }
180 
181 inline const BitRegister BitRegister::operator&(const BitRegister &rhs) const{
182  BitRegister result;
183  result.values = values & rhs.values;
184  return result;
185 }
186 
187 inline const BitRegister BitRegister::operator^(const BitRegister &rhs) const{
188  BitRegister result;
189  result.values = values^rhs.values;
190  return result;
191 }
192 
193 inline const BitRegister BitRegister::operator+(const BitRegister &rhs) const{
194  BitRegister result;
195  result.values = values + rhs.values;
196  return result;
197 }
198 
199 inline const BitRegister BitRegister::operator-(const BitRegister &rhs) const{
200  BitRegister result;
201  result.values = values - rhs.values;
202  return result;
203 }
204 
205 inline bool BitRegister::operator<(const BitRegister &rhs) const{
206  return values < rhs.values;
207 }
208 
209 inline bool BitRegister::operator>(const BitRegister &rhs) const{
210  return values > rhs.values;
211 }
212 
213 inline bool BitRegister::operator==(const BitRegister &rhs) const{
214  return values == rhs.values;
215 }
216 
217 inline void BitRegister::operator+=(const BitRegister &rhs){
218  values += rhs.values;
219 }
220 
221 inline void BitRegister::operator-=(const BitRegister &rhs){
222  values -= rhs.values;
223 }
224 
226  values++;
227  return *this;
228 }
229 
231  BitRegister returnValue;
232  returnValue = *this;
233  values++;
234  return returnValue;
235 }
236 
238  values--;
239  return *this;
240 }
241 
243  BitRegister returnValue;
244  returnValue = *this;
245  values--;
246  return returnValue;
247 }
248 
249 inline void BitRegister::operator=(const BitRegister &rhs){
250  if(this != &rhs)
251  values = rhs.values;
252 }
253 
254 inline void BitRegister::operator=(unsigned int rhs){
255  values = rhs;
256 }
257 
258 inline BitRegister BitRegister::operator<<(unsigned int rhs) const{
259  BitRegister result;
260  result.values = values << rhs;
261  return result;
262 }
263 
264 inline BitRegister BitRegister::operator>>(unsigned int rhs) const{
265  BitRegister result;
266  result.values = values >> rhs;
267  return result;
268 }
269 
270 inline void BitRegister::print() const{
271  for(int n = 8*sizeof(values)-1; n >= 0; n--)
272  Streams::out << (0x1 & (values >> n));
273  Streams::out << "\n";
274 }
275 
276 inline void BitRegister::setBit(unsigned int position, bool value){
277  values &= ~(1 << position);
278  values ^= (value << position);
279 }
280 
281 inline bool BitRegister::getBit(unsigned int position) const{
282  return (0x1 & (values >> position));
283 }
284 
285 inline void BitRegister::setValues(unsigned int values){
286  this->values = values;
287 }
288 
289 inline unsigned int BitRegister::getValues() const{
290  return values;
291 }
292 
293 inline bool BitRegister::toBool() const{
294  return values;
295 }
296 
297 inline unsigned int BitRegister::toUnsignedInt() const{
298  return values;
299 }
300 
301 inline void BitRegister::clear(){
302  values = 0;
303 }
304 
305 inline unsigned int BitRegister::getNumBits() const{
306  return 8*sizeof(values);
307 }
308 
309 inline unsigned int BitRegister::getNumOneBits() const{
310  unsigned int x = values;
311  x = x - ((x >> 1) & 0x55555555);
312  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
313  x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
314  x = x + (x >> 8);
315  x = x + (x >> 16);
316  return (x & 0x0000003F);
317 }
318 
320  return values & MOST_SIGNIFICANT_BIT_MASK;
321 }
322 
324  values |= MOST_SIGNIFICANT_BIT_MASK;
325 }
326 
328  values &= !MOST_SIGNIFICANT_BIT_MASK;
329 }
330 
332  return BitRegister(8*sizeof(unsigned int));
333 }
334 
335 inline std::string BitRegister::toString() const{
336  std::stringstream stream;
337  for(unsigned int n = getNumBits(); n > 0; n--)
338  stream << getBit(n - 1);
339 
340  return stream.str();
341 }
342 
343 inline std::ostream& operator<<(
344  std::ostream &stream,
345  const BitRegister &bitRegister
346 ){
347  stream << bitRegister.toString();
348 
349  return stream;
350 }
351 
352 }; //End of namespace TBTK
353 
354 #endif
unsigned int toUnsignedInt() const
Definition: BitRegister.h:297
unsigned int getNumBits() const
Definition: BitRegister.h:305
void setBit(unsigned int position, bool values)
Definition: BitRegister.h:276
const BitRegister operator^(const BitRegister &rhs) const
Definition: BitRegister.h:187
bool operator==(const BitRegister &rhs) const
Definition: BitRegister.h:213
std::string toString() const
Definition: BitRegister.h:335
BitRegister(unsigned int numBits=8 *sizeof(unsigned int))
BitRegister cloneStructure() const
Definition: BitRegister.h:331
Register of bits.
Definition: BitRegister.h:41
const BitRegister operator-(const BitRegister &rhs) const
Definition: BitRegister.h:199
bool getBit(unsigned int position) const
Definition: BitRegister.h:281
bool toBool() const
Definition: BitRegister.h:293
bool operator<(const BitRegister &rhs) const
Definition: BitRegister.h:205
const BitRegister operator+(const BitRegister &rhs) const
Definition: BitRegister.h:193
unsigned int getNumOneBits() const
Definition: BitRegister.h:309
static std::ostream out
Definition: Streams.h:70
void print() const
Definition: BitRegister.h:270
bool getMostSignificantBit() const
Definition: BitRegister.h:319
unsigned int getValues() const
Definition: BitRegister.h:289
Definition: Boolean.h:32
const BitRegister & operator++()
Definition: BitRegister.h:225
BitRegister operator>>(unsigned int rhs) const
Definition: BitRegister.h:264
void operator+=(const BitRegister &rhs)
Definition: BitRegister.h:217
const BitRegister & operator--()
Definition: BitRegister.h:237
void operator-=(const BitRegister &rhs)
Definition: BitRegister.h:221
void operator=(const BitRegister &rhs)
Definition: BitRegister.h:249
void clearMostSignificantBit()
Definition: BitRegister.h:327
void setMostSignificantBit()
Definition: BitRegister.h:323
const BitRegister operator|(const BitRegister &rhs) const
Definition: BitRegister.h:175
Streams for TBTK output.
bool operator>(const BitRegister &rhs) const
Definition: BitRegister.h:209
BitRegister operator<<(unsigned int rhs) const
Definition: BitRegister.h:258
void clear()
Definition: BitRegister.h:301
void setValues(unsigned int values)
Definition: BitRegister.h:285
const BitRegister operator &(const BitRegister &rhs) const