KaliVeda
Toolkit for HIC analysis
Loading...
Searching...
No Matches
Binary_t.h
1
2
3#ifndef __BINARY_T__
4#define __BINARY_T__
5
6#include "Rtypes.h"
7#include "Riostream.h"
8#include "TString.h"
9
10class Hexa_t;
11
104template < class T > class Binary_t {
105private:
106 UChar_t fNBitsRep; //number of bits used to represent value
107 UChar_t fNBits; //number of bits used to store value = kBitsPerByte*sizeof(T)
108 T fVal; //decimal used to store value
109
112
113 void init();
114
115public:
117 Binary_t(const T);
120 virtual ~ Binary_t()
121 {
122 };
123 void Set(const T);
124 void Set(const Char_t*);
125 T Value() const;
126 const Char_t* String(UChar_t nbits = 0);
127
128 Long64_t Max() const;
129
131 void SetBit(UChar_t, T);
134
135 void WriteSubvalue(const T, UChar_t msb, UChar_t nbits);
138 Binary_t < T >& operator=(const Binary_t < T >&);
139 Binary_t < T >& operator=(const T);
140 Binary_t < T >& operator=(const Char_t*);
143 operator Int_t() const
144 {
145 return (Int_t) Value();
146 };
147 operator T() const
148 {
149 return Value();
150 };
151 operator Float_t() const
152 {
153 return (Float_t) Value();
154 };
155 operator Double_t() const
156 {
157 return (Double_t) Value();
158 };
159 operator const char* ()
160 {
161 return String();
162 };
163
164 Hexa_t Hexa() const;
165
166 void SetNBits(UChar_t nbits)
167 {
168 fNBitsRep = nbits;
169 };
171 {
172 return (fNBitsRep);
173 };
174
190
191 Bool_t operator==(const Binary_t < T >&);
192 Bool_t operator!=(const Binary_t < T >&);
194
195 void Print(Option_t* opt = "") const;
196
197 ClassDef(Binary_t, 1) //a binary number
198};
199
209
210#include "Riostream.h"
211#include "TMath.h"
212#include "Hexa_t.h"
213#include "TString.h"
214
215template < class T > void Binary_t < T >::init()
216{
218 fNBits = (UChar_t)(kBitsPerByte * sizeof(T));
219}
220
221template < class T > Binary_t < T >::Binary_t()
222{
224 fNBitsRep = 0;
225 fVal = 0;
226 init();
227}
228
229template < class T > Binary_t < T >::Binary_t(const Binary_t < T >& b)
230{
232 init();
233 fNBitsRep = b.GetNBits();
234 fVal = b.Value();
235}
236
237template < class T > Binary_t < T >::Binary_t(const T val)
238{
240 fNBitsRep = 0;
241 fVal = val;
242 init();
243}
244
245template < class T > Binary_t < T >::Binary_t(const Char_t* val)
246{
248 Set(val);
249 fNBitsRep = 0;
250 init();
251}
252
253template < class T > void Binary_t < T >::Set(const T val)
254{
256 fVal = val;
257}
258
259template < class T > T Binary_t < T >::Value() const
260{
262 return (fVal);
263}
264
265template < class T > void Binary_t < T >::Set(const Char_t* val)
266{
270
271 fVal = 0;
272 Int_t nbits = (Int_t) strlen(val);
273 Int_t ibit = 0;
274 Int_t idx_min = nbits - fNBits;
275 idx_min = idx_min * (idx_min > 0);
276 for (Int_t i = nbits - 1; i >= idx_min; i--) {
277 if (val[i] - '0') { //if a '1' is present here...
278 fVal += (T)(1 << ibit);
279 }
280 ibit++;
281 }
282}
283
284template < class T > const Char_t* Binary_t < T >::String(UChar_t nbits)
285{
297 UChar_t setbits = 0;
298 if (!Value()) {
299 fDumString = "0";
300 setbits = 1;
301 }
302 else {
303 fDumString = "";
304 T cont = Value();
305 while (cont) {
306 if (cont % 2)
307 fDumString.Prepend('1');
308 else
309 fDumString.Prepend('0');
310 setbits++;
311 cont /= 2;
312 }
313 }
317 Int_t lzos =
318 (nbits ? (Int_t) nbits -
319 (Int_t) setbits : (GetNBits() ? (Int_t) GetNBits() -
320 (Int_t) setbits : 0));
321 if (lzos > 0) {
323 fDumString.Prepend('0', lzos);
324 }
325 else if (lzos < 0) {
327 fDumString.Remove(0, -lzos);
328 }
329 return fDumString.Data();
330}
331
332template < class T > Binary_t < T >& Binary_t < T >::operator=(const T val)
333{
335 Set(val);
336 return (*this);
337}
338
339template < class T > Binary_t < T >& Binary_t <
340T >::operator=(const Char_t* val)
341{
343 Set(val);
344 return (*this);
345}
346
353
354template < class T > Binary_t < T >& Binary_t <
355T >::operator=(const Binary_t& val)
356{
358 Set(val.Value());
359 return (*this);
360}
361
362template <class T> Bool_t Binary_t<T>::operator==(const Binary_t < T >& b2)
363{
365 return (fVal == b2.fVal);
366}
367
368template <class T> Bool_t Binary_t<T>::operator!=(const Binary_t < T >& b2)
369{
371 return (fVal != b2.fVal);
372}
373
374template <class T> Bool_t Binary_t<T>::operator!=(const Char_t* b2)
375{
377 Binary_t<T> Bb2(b2);
378 return ((*this) != Bb2);
379}
380
418
420
421template <class T> Binary_t<T> Binary_t<T>::operator|(const Binary_t<T>& b1)
422{
424 Binary_t<T> tmp(fVal | b1.Value());
425 return tmp;
426}
427
462template <class T> Binary_t<T> Binary_t<T>::operator&(const Binary_t<T>& b1)
463{
465 Binary_t<T> tmp(fVal & b1.Value());
466 return tmp;
467}
468
498
499template < class T > void Binary_t < T >::SetBit(UChar_t nbit)
500{
503
504 SETBIT(fVal, nbit);
505}
506
507template < class T > void Binary_t < T >::SetBit(UChar_t nbit, T val)
508{
510
511 if (TESTBIT(val, nbit))
512 SetBit(nbit);
513 else
514 ResetBit(nbit);
515}
516
517template < class T > void Binary_t < T >::ResetBit(UChar_t nbit)
518{
521
522 CLRBIT(fVal, nbit);
523}
524
525template < class T > Bool_t Binary_t < T >::TestBit(UChar_t nbit)
526{
529
530 return TESTBIT(fVal, nbit);
531}
532
533template < class T > Hexa_t Binary_t < T >::Hexa() const
534{
536 return Hexa_t((Long64_t) Value());
537}
538
539template < class T > void Binary_t < T >::WriteSubvalue(const T val,
540 UChar_t msb,
541 UChar_t nbits)
542{
547
549 Int_t _fNBits, _msb, _nbits;
550 _fNBits = (Int_t) fNBits;
551 _msb = (Int_t) msb;
552 _nbits = (Int_t) nbits;
553 if (_nbits < 1 || _nbits > _fNBits || _msb > (_fNBits - 1)
554 || (_msb - _nbits + 1) < 0) {
555 std::cout << "Error in <Binary_t<T>::WriteSubvalue> : ";
556 if (_nbits < 1)
557 std::cout << "nbits<1";
558 if (_nbits > _fNBits)
559 std::cout << "nbits(" << _nbits << ")>fNBits(" << _fNBits << ")";
560 if (_msb > (_fNBits - 1))
561 std::cout << "msb(" << _msb << ") > fNBits-1(" << _fNBits - 1 << ")";
562 if ((_msb - _nbits + 1) < 0)
563 std::cout << "(msb-nbits+1) < 0 : msb=" << _msb << " nbits=" << _nbits;
564 std::cout << std::endl;
565 return;
566 }
568 Binary_t < T > tmp(val);
570 String(fNBits); //NB result is in fDumString !!
572 fDumString.Replace((_fNBits - _msb - 1), nbits, tmp.String(nbits));
574 Set(fDumString.Data());
575}
576
577template < class T > void Binary_t < T >::Print(Option_t*) const
578{
579 std::cout << "Binary number : " << const_cast<Binary_t<T>*>(this)->String() << " : fNBits=" << (int) fNBits
580 << " fNBitsRep=" << (int) fNBitsRep << " fVal=" << (Long64_t)
581 Value() << std::endl;
582}
583
584template < class T > T Binary_t < T >::Subvalue(UChar_t firstbit,
585 UChar_t nbits)
586{
592
594 Int_t _firstbit, _nbits;
595 _firstbit = (Int_t) firstbit;
596 _nbits = (Int_t) nbits;
598 _nbits = ((_firstbit - _nbits + 1) < 0 ? _firstbit + 1 : _nbits);
599
601 String(_firstbit + 1); //result is in fDumString!!
603 fDumString2 = fDumString(0, _nbits);
604
605 Binary_t < T > tmp;
606 tmp.Set(fDumString2.Data());
607 return (tmp.Value());
608}
609
610template < class T > Long64_t Binary_t < T >::Max() const
611{
613 return (Long64_t)pow(2.0, (int)fNBits) - 1;
614}
615
618typedef Binary_t < UChar_t > Binary8_t;
619typedef Binary_t < UShort_t > Binary16_t;
620typedef Binary_t < UInt_t > Binary32_t;
621typedef Binary_t < Long64_t > Binary64_t;
622#endif
int Int_t
bool Bool_t
unsigned char UChar_t
constexpr ULong_t kBitsPerByte
char Char_t
float Float_t
double Double_t
const char Option_t
#define ClassDef(name, id)
#define SETBIT(n, i)
#define TESTBIT(n, i)
#define CLRBIT(n, i)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
Binary numbers, bit manipulations, etc.
Definition Binary_t.h:104
void Set(const Char_t *)
Definition Binary_t.h:265
Bool_t operator!=(const Binary_t< T > &)
Definition Binary_t.h:368
void ResetBit(UChar_t)
Definition Binary_t.h:517
void SetBit(UChar_t, T)
Definition Binary_t.h:507
Binary_t(const Char_t *)
Definition Binary_t.h:245
void SetNBits(UChar_t nbits)
Definition Binary_t.h:166
T Subvalue(UChar_t msb, UChar_t nbits)
Definition Binary_t.h:584
Long64_t Max() const
Definition Binary_t.h:610
TString fDumString
Definition Binary_t.h:110
UChar_t GetNBits() const
Definition Binary_t.h:170
UChar_t fNBits
Definition Binary_t.h:107
Binary_t< T > operator&(const Binary_t< T > &b1)
Definition Binary_t.h:462
void SetBit(UChar_t)
Definition Binary_t.h:499
Binary_t< T > & operator=(const T)
Definition Binary_t.h:332
void Print(Option_t *opt="") const
Definition Binary_t.h:577
UChar_t fNBitsRep
Definition Binary_t.h:106
Bool_t operator!=(const Char_t *)
Definition Binary_t.h:374
Binary_t< T > & operator=(const Char_t *)
Definition Binary_t.h:340
Binary_t< T > & operator=(const Binary_t< T > &)
equals operators
Definition Binary_t.h:355
const Char_t * String(UChar_t nbits=0)
Definition Binary_t.h:284
Binary_t(const Binary_t &)
Definition Binary_t.h:229
Binary_t< T > operator|(const Binary_t< T > &b1)
-----------------------------------------------—bitwise OR operators
Definition Binary_t.h:421
Bool_t operator==(const Binary_t< T > &)
Definition Binary_t.h:362
Binary_t(const T)
Definition Binary_t.h:237
T Value() const
Definition Binary_t.h:259
TString fDumString2
dummy, used by String method
Definition Binary_t.h:111
Hexa_t Hexa() const
Definition Binary_t.h:533
void Set(const T)
Definition Binary_t.h:253
void WriteSubvalue(const T, UChar_t msb, UChar_t nbits)
Definition Binary_t.h:539
void init()
dummy, used by WriteSubValue method
Definition Binary_t.h:215
Bool_t TestBit(UChar_t)
Definition Binary_t.h:525
Hexadecimal numbers.
Definition Hexa_t.h:15
long long Long64_t
RVec< PromoteTypes< T0, T1 > > pow(const RVec< T0 > &v, const T1 &y)
double T(double x)
void init()
const char * String