KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
Binary_t.h
Go to the documentation of this file.
1 
3 #ifndef __BINARY_T__
4 #define __BINARY_T__
5 
6 #include "Rtypes.h"
7 #include "Riostream.h"
8 #include "TString.h"
9 
10 class Hexa_t;
11 
103 template < class T > class Binary_t {
104 private:
105  UChar_t fNBitsRep; //number of bits used to represent value
106  UChar_t fNBits; //number of bits used to store value = kBitsPerByte*sizeof(T)
107  T fVal; //decimal used to store value
108 
111 
112  void init();
113 
114 public:
116  Binary_t(const T);
117  Binary_t(const Char_t*);
119  virtual ~ Binary_t()
120  {
121  };
122  void Set(const T);
123  void Set(const Char_t*);
124  T Value() const;
125  const Char_t* String(UChar_t nbits = 0);
126 
127  Long64_t Max() const;
128 
130  void SetBit(UChar_t, T);
133 
134  void WriteSubvalue(const T, UChar_t msb, UChar_t nbits);
135  T Subvalue(UChar_t msb, UChar_t nbits);
142  operator Int_t() const
143  {
144  return (Int_t) Value();
145  };
146  operator T() const
147  {
148  return Value();
149  };
150  operator Float_t() const
151  {
152  return (Float_t) Value();
153  };
154  operator Double_t() const
155  {
156  return (Double_t) Value();
157  };
158  operator const char* ()
159  {
160  return String();
161  };
162 
163  Hexa_t Hexa() const;
164 
165  void SetNBits(UChar_t nbits)
166  {
167  fNBitsRep = nbits;
168  };
170  {
171  return (fNBitsRep);
172  };
173 
189 
193 
194  void Print(Option_t* opt = "") const;
195 
196  ClassDef(Binary_t, 1) //a binary number
197 };
198 
208 
209 #include "Riostream.h"
210 #include "TMath.h"
211 #include "Hexa_t.h"
212 #include "TString.h"
213 
214 template < class T > void Binary_t < T >::init()
215 {
217  fNBits = (UChar_t)(kBitsPerByte * sizeof(T));
218 }
219 
220 template < class T > Binary_t < T >::Binary_t()
221 {
223  fNBitsRep = 0;
224  fVal = 0;
225  init();
226 }
227 
228 template < class T > Binary_t < T >::Binary_t(const Binary_t < T >& b)
229 {
231  init();
232  fNBitsRep = b.GetNBits();
233  fVal = b.Value();
234 }
235 
236 template < class T > Binary_t < T >::Binary_t(const T val)
237 {
239  fNBitsRep = 0;
240  fVal = val;
241  init();
242 }
243 
244 template < class T > Binary_t < T >::Binary_t(const Char_t* val)
245 {
247  Set(val);
248  fNBitsRep = 0;
249  init();
250 }
251 
252 template < class T > void Binary_t < T >::Set(const T val)
253 {
255  fVal = val;
256 }
257 
258 template < class T > T Binary_t < T >::Value() const
259 {
261  return (fVal);
262 }
263 
264 template < class T > void Binary_t < T >::Set(const Char_t* val)
265 {
269 
270  fVal = 0;
271  Int_t nbits = (Int_t) strlen(val);
272  Int_t ibit = 0;
273  Int_t idx_min = nbits - fNBits;
274  idx_min = idx_min * (idx_min > 0);
275  for (Int_t i = nbits - 1; i >= idx_min; i--) {
276  if (val[i] - '0') { //if a '1' is present here...
277  fVal += (T)(1 << ibit);
278  }
279  ibit++;
280  }
281 }
282 
283 template < class T > const Char_t* Binary_t < T >::String(UChar_t nbits)
284 {
296  UChar_t setbits = 0;
297  if (!Value()) {
298  fDumString = "0";
299  setbits = 1;
300  }
301  else {
302  fDumString = "";
303  T cont = Value();
304  while (cont) {
305  if (cont % 2)
306  fDumString.Prepend('1');
307  else
308  fDumString.Prepend('0');
309  setbits++;
310  cont /= 2;
311  }
312  }
316  Int_t lzos =
317  (nbits ? (Int_t) nbits -
318  (Int_t) setbits : (GetNBits() ? (Int_t) GetNBits() -
319  (Int_t) setbits : 0));
320  if (lzos > 0) {
322  fDumString.Prepend('0', lzos);
323  }
324  else if (lzos < 0) {
326  fDumString.Remove(0, -lzos);
327  }
328  return fDumString.Data();
329 }
330 
331 template < class T > Binary_t < T >& Binary_t < T >::operator=(const T val)
332 {
334  Set(val);
335  return (*this);
336 }
337 
338 template < class T > Binary_t < T >& Binary_t <
339 T >::operator=(const Char_t* val)
340 {
342  Set(val);
343  return (*this);
344 }
345 
352 
353 template < class T > Binary_t < T >& Binary_t <
354 T >::operator=(const Binary_t& val)
355 {
357  Set(val.Value());
358  return (*this);
359 }
360 
361 template <class T> Bool_t Binary_t<T>::operator==(const Binary_t < T >& b2)
362 {
364  return (fVal == b2.fVal);
365 }
366 
367 template <class T> Bool_t Binary_t<T>::operator!=(const Binary_t < T >& b2)
368 {
370  return (fVal != b2.fVal);
371 }
372 
373 template <class T> Bool_t Binary_t<T>::operator!=(const Char_t* b2)
374 {
376  Binary_t<T> Bb2(b2);
377  return ((*this) != Bb2);
378 }
379 
417 
419 
420 template <class T> Binary_t<T> Binary_t<T>::operator|(const Binary_t<T>& b1)
421 {
423  Binary_t<T> tmp(fVal | b1.Value());
424  return tmp;
425 }
426 
461 template <class T> Binary_t<T> Binary_t<T>::operator&(const Binary_t<T>& b1)
462 {
464  Binary_t<T> tmp(fVal & b1.Value());
465  return tmp;
466 }
467 
497 
498 template < class T > void Binary_t < T >::SetBit(UChar_t nbit)
499 {
502 
503  SETBIT(fVal, nbit);
504 }
505 
506 template < class T > void Binary_t < T >::SetBit(UChar_t nbit, T val)
507 {
509 
510  if (TESTBIT(val, nbit))
511  SetBit(nbit);
512  else
513  ResetBit(nbit);
514 }
515 
516 template < class T > void Binary_t < T >::ResetBit(UChar_t nbit)
517 {
520 
521  CLRBIT(fVal, nbit);
522 }
523 
524 template < class T > Bool_t Binary_t < T >::TestBit(UChar_t nbit)
525 {
528 
529  return TESTBIT(fVal, nbit);
530 }
531 
532 template < class T > Hexa_t Binary_t < T >::Hexa() const
533 {
535  return Hexa_t((Long64_t) Value());
536 }
537 
538 template < class T > void Binary_t < T >::WriteSubvalue(const T val,
539  UChar_t msb,
540  UChar_t nbits)
541 {
546 
548  Int_t _fNBits, _msb, _nbits;
549  _fNBits = (Int_t) fNBits;
550  _msb = (Int_t) msb;
551  _nbits = (Int_t) nbits;
552  if (_nbits < 1 || _nbits > _fNBits || _msb > (_fNBits - 1)
553  || (_msb - _nbits + 1) < 0) {
554  std::cout << "Error in <Binary_t<T>::WriteSubvalue> : ";
555  if (_nbits < 1)
556  std::cout << "nbits<1";
557  if (_nbits > _fNBits)
558  std::cout << "nbits(" << _nbits << ")>fNBits(" << _fNBits << ")";
559  if (_msb > (_fNBits - 1))
560  std::cout << "msb(" << _msb << ") > fNBits-1(" << _fNBits - 1 << ")";
561  if ((_msb - _nbits + 1) < 0)
562  std::cout << "(msb-nbits+1) < 0 : msb=" << _msb << " nbits=" << _nbits;
563  std::cout << std::endl;
564  return;
565  }
567  Binary_t < T > tmp(val);
569  String(fNBits); //NB result is in fDumString !!
571  fDumString.Replace((_fNBits - _msb - 1), nbits, tmp.String(nbits));
573  Set(fDumString.Data());
574 }
575 
576 template < class T > void Binary_t < T >::Print(Option_t*) const
577 {
578  std::cout << "Binary number : " << const_cast<Binary_t<T>*>(this)->String() << " : fNBits=" << (int) fNBits
579  << " fNBitsRep=" << (int) fNBitsRep << " fVal=" << (Long64_t)
580  Value() << std::endl;
581 }
582 
583 template < class T > T Binary_t < T >::Subvalue(UChar_t firstbit,
584  UChar_t nbits)
585 {
591 
593  Int_t _firstbit, _nbits;
594  _firstbit = (Int_t) firstbit;
595  _nbits = (Int_t) nbits;
597  _nbits = ((_firstbit - _nbits + 1) < 0 ? _firstbit + 1 : _nbits);
598 
600  String(_firstbit + 1); //result is in fDumString!!
602  fDumString2 = fDumString(0, _nbits);
603 
604  Binary_t < T > tmp;
605  tmp.Set(fDumString2.Data());
606  return (tmp.Value());
607 }
608 
609 template < class T > Long64_t Binary_t < T >::Max() const
610 {
612  return (Long64_t)pow(2.0, (int)fNBits) - 1;
613 }
614 
621 #endif
Binary_t< UChar_t > Binary8_t
Definition: Binary_t.h:617
Binary_t< UShort_t > Binary16_t
Definition: Binary_t.h:618
Binary_t< UInt_t > Binary32_t
Definition: Binary_t.h:619
Binary_t< Long64_t > Binary64_t
Definition: Binary_t.h:620
int Int_t
#define b(i)
unsigned char UChar_t
char Char_t
bool Bool_t
const ULong_t kBitsPerByte
double Double_t
float Float_t
const char Option_t
#define ClassDef(name, id)
#define SETBIT(n, i)
#define TESTBIT(n, i)
#define CLRBIT(n, i)
double pow(double, double)
Binary numbers, bit manipulations, etc.
Definition: Binary_t.h:103
void Set(const Char_t *)
Definition: Binary_t.h:264
Bool_t operator!=(const Binary_t< T > &)
Definition: Binary_t.h:367
void ResetBit(UChar_t)
Definition: Binary_t.h:516
void SetBit(UChar_t, T)
Definition: Binary_t.h:506
Binary_t(const Char_t *)
Definition: Binary_t.h:244
void SetNBits(UChar_t nbits)
Definition: Binary_t.h:165
T Subvalue(UChar_t msb, UChar_t nbits)
Definition: Binary_t.h:583
Long64_t Max() const
Definition: Binary_t.h:609
TString fDumString
Definition: Binary_t.h:109
UChar_t GetNBits() const
Definition: Binary_t.h:169
UChar_t fNBits
Definition: Binary_t.h:106
Binary_t< T > operator&(const Binary_t< T > &b1)
Definition: Binary_t.h:461
virtual ~ Binary_t()
Definition: Binary_t.h:119
void SetBit(UChar_t)
Definition: Binary_t.h:498
Binary_t< T > & operator=(const T)
Definition: Binary_t.h:331
void Print(Option_t *opt="") const
Definition: Binary_t.h:576
UChar_t fNBitsRep
Definition: Binary_t.h:105
Bool_t operator!=(const Char_t *)
Definition: Binary_t.h:373
Binary_t< T > & operator=(const Char_t *)
Definition: Binary_t.h:339
Binary_t< T > & operator=(const Binary_t< T > &)
equals operators
Definition: Binary_t.h:354
const Char_t * String(UChar_t nbits=0)
Definition: Binary_t.h:283
Binary_t(const Binary_t &)
Definition: Binary_t.h:228
Binary_t< T > operator|(const Binary_t< T > &b1)
-----------------------------------------------—bitwise OR operators
Definition: Binary_t.h:420
Bool_t operator==(const Binary_t< T > &)
Definition: Binary_t.h:361
Binary_t()
Definition: Binary_t.h:220
Binary_t(const T)
Definition: Binary_t.h:236
T Value() const
Definition: Binary_t.h:258
TString fDumString2
dummy, used by String method
Definition: Binary_t.h:110
Hexa_t Hexa() const
Definition: Binary_t.h:532
void Set(const T)
Definition: Binary_t.h:252
void WriteSubvalue(const T, UChar_t msb, UChar_t nbits)
Definition: Binary_t.h:538
void init()
dummy, used by WriteSubValue method
Definition: Binary_t.h:214
Bool_t TestBit(UChar_t)
Definition: Binary_t.h:524
Hexadecimal numbers.
Definition: Hexa_t.h:14
long long Long64_t
double T(double x)
const char * String