TBTK
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 namespace TBTK{
29 
31 public:
33  BitRegister(unsigned int numBits = 8*sizeof(unsigned int));
34 
36  BitRegister(const BitRegister &bitRegister);
37 
39  ~BitRegister();
40 
42  const BitRegister operator|(const BitRegister &rhs) const;
43 
45  const BitRegister operator&(const BitRegister &rhs) const;
46 
48  const BitRegister operator^(const BitRegister &rhs) const;
49 
51  const BitRegister operator+(const BitRegister &rhs) const;
52 
54  const BitRegister operator-(const BitRegister &rhs) const;
55 
57  bool operator<(const BitRegister &rhs) const;
58 
60  bool operator>(const BitRegister &rhs) const;
61 
63  bool operator==(const BitRegister &rhs) const;
64 
66  void operator+=(const BitRegister &rhs);
67 
69  void operator-=(const BitRegister &rhs);
70 
72  const BitRegister& operator++();
73 
75  const BitRegister operator++(int);
76 
78  const BitRegister& operator--();
79 
81  const BitRegister operator--(int);
82 
84  void operator=(const BitRegister &rhs);
85 
87  void operator=(unsigned int rhs);
88 
90  BitRegister operator<<(unsigned int rhs) const;
91 
93  BitRegister operator>>(unsigned int rhs) const;
94 
96  void setBit(unsigned int position, bool values);
97 
99  bool getBit(unsigned int position) const;
100 
102  void setValues(unsigned int values);
103 
105  unsigned int getValues() const;
106 
109  bool toBool() const;
110 
112  unsigned int toUnsignedInt() const;
113 
115  void clear();
116 
118  void print() const;
119 
121  unsigned int getNumBits() const;
122 
124  unsigned int getNumOneBits() const;
125 
127  bool getMostSignificantBit() const;
128 
130  void setMostSignificantBit();
131 
134 
138  BitRegister cloneStructure() const;
139 
141  unsigned int getAsUnsignedInt() const;
142 private:
144  unsigned int values;
145 
147  static constexpr unsigned int MOST_SIGNIFICANT_BIT_MASK
148  = (unsigned int)0x1 << (8*sizeof(unsigned int)-1);
149 };
150 
151 inline const BitRegister BitRegister::operator|(const BitRegister &rhs) const{
152  BitRegister result;
153  result.values = values | rhs.values;
154  return result;
155 }
156 
157 inline const BitRegister BitRegister::operator&(const BitRegister &rhs) const{
158  BitRegister result;
159  result.values = values & rhs.values;
160  return result;
161 }
162 
163 inline const BitRegister BitRegister::operator^(const BitRegister &rhs) const{
164  BitRegister result;
165  result.values = values^rhs.values;
166  return result;
167 }
168 
169 inline const BitRegister BitRegister::operator+(const BitRegister &rhs) const{
170  BitRegister result;
171  result.values = values + rhs.values;
172  return result;
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 bool BitRegister::operator<(const BitRegister &rhs) const{
182  return values < rhs.values;
183 }
184 
185 inline bool BitRegister::operator>(const BitRegister &rhs) const{
186  return values > rhs.values;
187 }
188 
189 inline bool BitRegister::operator==(const BitRegister &rhs) const{
190  return values == rhs.values;
191 }
192 
193 inline void BitRegister::operator+=(const BitRegister &rhs){
194  values += rhs.values;
195 }
196 
197 inline void BitRegister::operator-=(const BitRegister &rhs){
198  values -= rhs.values;
199 }
200 
202  values++;
203  return *this;
204 }
205 
207  unsigned int returnValue = values;
208  values++;
209  return returnValue;
210 }
211 
213  values--;
214  return *this;
215 }
216 
218  unsigned int returnValue = values;
219  values--;
220  return returnValue;
221 }
222 
223 inline void BitRegister::operator=(const BitRegister &rhs){
224  if(this != &rhs)
225  values = rhs.values;
226 }
227 
228 inline void BitRegister::operator=(unsigned int rhs){
229  values = rhs;
230 }
231 
232 inline BitRegister BitRegister::operator<<(unsigned int rhs) const{
233  BitRegister result;
234  result.values = values << rhs;
235  return result;
236 }
237 
238 inline BitRegister BitRegister::operator>>(unsigned int rhs) const{
239  BitRegister result;
240  result.values = values >> rhs;
241  return result;
242 }
243 
244 inline void BitRegister::print() const{
245  for(int n = 8*sizeof(values)-1; n >= 0; n--)
246  Streams::out << (0x1 & (values >> n));
247  Streams::out << "\n";
248 }
249 
250 inline void BitRegister::setBit(unsigned int position, bool value){
251  values &= ~(1 << position);
252  values ^= (value << position);
253 }
254 
255 inline bool BitRegister::getBit(unsigned int position) const{
256  return (0x1 & (values >> position));
257 }
258 
259 inline void BitRegister::setValues(unsigned int values){
260  this->values = values;
261 }
262 
263 inline unsigned int BitRegister::getValues() const{
264  return values;
265 }
266 
267 inline bool BitRegister::toBool() const{
268  return values;
269 }
270 
271 inline unsigned int BitRegister::toUnsignedInt() const{
272  return values;
273 }
274 
275 inline void BitRegister::clear(){
276  values = 0;
277 }
278 
279 inline unsigned int BitRegister::getNumBits() const{
280  return 8*sizeof(values);
281 }
282 
283 inline unsigned int BitRegister::getNumOneBits() const{
284  unsigned int x = values;
285  x = x - ((x >> 1) & 0x55555555);
286  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
287  x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
288  x = x + (x >> 8);
289  x = x + (x >> 16);
290  return (x & 0x0000003F);
291 }
292 
294  return values & MOST_SIGNIFICANT_BIT_MASK;
295 }
296 
298  values |= MOST_SIGNIFICANT_BIT_MASK;
299 }
300 
302  values &= !MOST_SIGNIFICANT_BIT_MASK;
303 }
304 
306  return BitRegister(8*sizeof(unsigned int));
307 }
308 
309 }; //End of namespace TBTK
310 
311 #endif
void setBit(unsigned int position, bool values)
Definition: BitRegister.h:250
bool operator<(const BitRegister &rhs) const
Definition: BitRegister.h:181
BitRegister operator>>(unsigned int rhs) const
Definition: BitRegister.h:238
BitRegister(unsigned int numBits=8 *sizeof(unsigned int))
void print() const
Definition: BitRegister.h:244
Definition: BitRegister.h:30
const BitRegister operator^(const BitRegister &rhs) const
Definition: BitRegister.h:163
const BitRegister operator|(const BitRegister &rhs) const
Definition: BitRegister.h:151
bool toBool() const
Definition: BitRegister.h:267
bool getBit(unsigned int position) const
Definition: BitRegister.h:255
const BitRegister operator&(const BitRegister &rhs) const
Definition: BitRegister.h:157
const BitRegister operator+(const BitRegister &rhs) const
Definition: BitRegister.h:169
unsigned int getAsUnsignedInt() const
bool getMostSignificantBit() const
Definition: BitRegister.h:293
static std::ostream out
Definition: Streams.h:36
unsigned int toUnsignedInt() const
Definition: BitRegister.h:271
Definition: ModelFactory.h:35
unsigned int getNumOneBits() const
Definition: BitRegister.h:283
const BitRegister & operator++()
Definition: BitRegister.h:201
void operator+=(const BitRegister &rhs)
Definition: BitRegister.h:193
const BitRegister & operator--()
Definition: BitRegister.h:212
bool operator>(const BitRegister &rhs) const
Definition: BitRegister.h:185
void operator-=(const BitRegister &rhs)
Definition: BitRegister.h:197
const BitRegister operator-(const BitRegister &rhs) const
Definition: BitRegister.h:175
void operator=(const BitRegister &rhs)
Definition: BitRegister.h:223
void clearMostSignificantBit()
Definition: BitRegister.h:301
BitRegister operator<<(unsigned int rhs) const
Definition: BitRegister.h:232
void setMostSignificantBit()
Definition: BitRegister.h:297
bool operator==(const BitRegister &rhs) const
Definition: BitRegister.h:189
Streams for TBTK output.
BitRegister cloneStructure() const
Definition: BitRegister.h:305
void clear()
Definition: BitRegister.h:275
unsigned int getValues() const
Definition: BitRegister.h:263
void setValues(unsigned int values)
Definition: BitRegister.h:259
unsigned int getNumBits() const
Definition: BitRegister.h:279