KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
List of all members | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Binary_t< T > Class Template Reference

template<class T>
class Binary_t< T >

Binary numbers, bit manipulations, etc.

Classes representing binary numbers with 8, 16, 32 or 64 bits: Binary8_t, Binary16_t, Binary32_t, Binary64_t

The following are valid initialisations:

BinaryX_t a,b,c; Hexa_t d;
a = 26;
b = "110001";
c = d;
BinaryX_t e("1111");
#define d(i)
#define b(i)
#define c(i)
#define e(i)
Hexadecimal numbers.
Definition: Hexa_t.h:14
auto * a

Use Value() to obtain the decimal value of the number.

Use String() to obtain a string containing the binary representation. Default behaviour is to use the smallest number of bits necessary (single '0' for zero).

If you use SetNBits() to set the number of bits, any value, even 0, will always be represented using that number of bits. Use SetNBits(0) to reset to default behaviour. You can also impose the number of bits by calling String() with the required number.

NOTA BENE: the number of bits affects only the representation of the number as a string of '1's and '0's. It does not in any way affect either the current value of the number or its maximum possible value (given by Max()).

BIT NUMBERING CONVENTION

It is the same as the ROOT macro BIT(i) i.e.

Number = 01101110
Bit = 76543210

SetBit(UChar_t nbit, T val)

Set bit 'nbit' to 1 (the least significant bit is numbered 0). If "val" is given, bit 'nbit' is set to the same value as the equivalent bit in 'val'

ResetBit(UChar_t nbit)

Set bit 'nbit' to 0 (the least significant bit is numbered 0).

TestBit(UChar_t nbit)

Returns kTRUE if bit 'nbit' is equal to '1' (the least significant bit is numbered 0).

Hexa()

Returns number in its hexadecimal form (see Hexa_t)

WriteSubvalue(const T val, UChar_t msb, UChar_t nbits)

Write the value 'val' in the 'nbits' bits from bit 'msb' (Most Significant Bit) to bit (msb-nbits+1).

i.e. suppose b is an 8-bit binary number and we want to write the decimal value 10 in the 4 bits from bit 2 to bit 5 (remember that the bits are numbered from right to left, LSB=bit 0).

If b="00000000" and we do b.WriteSubvalue(10, 5, 4), we will have b="00101000"

Error if the following conditions are not respected:

1 <= nbits <= fNBits 0 <= msb <= (fNBits-1) (msb-nbits+1)>=0
UChar_t fNBits
Definition: Binary_t.h:106

Subvalue(UChar_t firstbit, UChar_t nbits)

Returns the decimal value of a part of this binary number. ('firstbit' is equivalent to 'msb' argument in WriteSubvalue).

The bits (firstbit, firstbit-1, firstbit-2, ... , firstbit-nbits+1) [see BIT NUMBERING CONVENTION] are used.

If firstbit-nbits+1 < 0 (i.e. there are not enough bits in the number) we use all the bits from firstbit down to bit 0.

Max()

Returns maximum value that can be stored in number, i.e. when all bits = 1

Print()

Print following info: string representation of binary number, number of bits, optional number of bits used for representation (default value 0), decimal value of number.

Definition at line 103 of file Binary_t.h.

Public Member Functions

 Binary_t ()
 
 Binary_t (const Binary_t &)
 
 Binary_t (const Char_t *)
 
 Binary_t (const T)
 
virtual ~ Binary_t ()
 
UChar_t GetNBits () const
 
Hexa_t Hexa () const
 
Long64_t Max () const
 
 operator const char * ()
 
 operator Double_t () const
 
 operator Float_t () const
 
 operator Int_t () const
 
 operator T () const
 
Bool_t operator!= (const Binary_t< T > &)
 
Bool_t operator!= (const Char_t *)
 
Binary_t< Toperator& (const Binary_t< T > &b1)
 
Binary_t< T > & operator= (const Binary_t< T > &)
 equals operators More...
 
Binary_t< T > & operator= (const Char_t *)
 
Binary_t< T > & operator= (const T)
 
Bool_t operator== (const Binary_t< T > &)
 
Binary_t< Toperator| (const Binary_t< T > &b1)
 -----------------------------------------------—bitwise OR operators More...
 
void Print (Option_t *opt="") const
 
void ResetBit (UChar_t)
 
void Set (const Char_t *)
 
void Set (const T)
 
void SetBit (UChar_t)
 
void SetBit (UChar_t, T)
 
void SetNBits (UChar_t nbits)
 
const Char_tString (UChar_t nbits=0)
 
T Subvalue (UChar_t msb, UChar_t nbits)
 
Bool_t TestBit (UChar_t)
 
T Value () const
 
void WriteSubvalue (const T, UChar_t msb, UChar_t nbits)
 

Private Member Functions

void init ()
 dummy, used by WriteSubValue method More...
 

Private Attributes

TString fDumString
 
TString fDumString2
 dummy, used by String method More...
 
UChar_t fNBits
 
UChar_t fNBitsRep
 
T fVal
 

#include <Binary_t.h>

Constructor & Destructor Documentation

◆ Binary_t() [1/4]

template<class T >
Binary_t< T >::Binary_t

Default constructor

Definition at line 220 of file Binary_t.h.

◆ Binary_t() [2/4]

template<class T >
Binary_t< T >::Binary_t ( const T  val)

Create binary number corresponding to decimal value "val"

Definition at line 236 of file Binary_t.h.

◆ Binary_t() [3/4]

template<class T >
Binary_t< T >::Binary_t ( const Char_t val)

Create binary number from string containing binary value

Definition at line 244 of file Binary_t.h.

◆ Binary_t() [4/4]

template<class T >
Binary_t< T >::Binary_t ( const Binary_t< T > &  b)

copy constructor

Definition at line 228 of file Binary_t.h.

◆ ~ Binary_t()

template<class T >
virtual Binary_t< T >::~ Binary_t ( )
inlinevirtual

Definition at line 119 of file Binary_t.h.

Member Function Documentation

◆ GetNBits()

template<class T >
UChar_t Binary_t< T >::GetNBits ( ) const
inline

Definition at line 169 of file Binary_t.h.

◆ Hexa()

template<class T >
Hexa_t Binary_t< T >::Hexa

Returns number in its hexadecimal form

Definition at line 532 of file Binary_t.h.

◆ init()

template<class T >
void Binary_t< T >::init ( void  )
private

dummy, used by WriteSubValue method

addtion Binary_t operator+(const Long64_t l1, const Binary_t& b2); Binary_t operator+(const Char_t* c1, const Binary_t& b2); bitwise OR Binary_t operator|(const Long64_t l1, const Binary_t& b2); Binary_t operator|(const Char_t* c1, const Binary_t& b2); //bitwise AND Binary_t operator&(const Long64_t l1, const Binary_t& b2); Binary_t operator&(const Char_t* c1, const Binary_t& b2);

Default initialisation; set fNBits = kBitsPerByte*sizeof(T)

Definition at line 214 of file Binary_t.h.

◆ Max()

template<class T >
Long64_t Binary_t< T >::Max

Returns maximum value that can be stored in number, i.e. when all bits = 1

Definition at line 609 of file Binary_t.h.

◆ operator const char *()

template<class T >
Binary_t< T >::operator const char * ( )
inline

Definition at line 158 of file Binary_t.h.

◆ operator Double_t()

template<class T >
Binary_t< T >::operator Double_t ( ) const
inline

Definition at line 154 of file Binary_t.h.

◆ operator Float_t()

template<class T >
Binary_t< T >::operator Float_t ( ) const
inline

Definition at line 150 of file Binary_t.h.

◆ operator Int_t()

template<class T >
Binary_t< T >::operator Int_t ( ) const
inline

Binary_t<T>& operator=(const Hexa_t&); convertors

Definition at line 142 of file Binary_t.h.

◆ operator T()

template<class T >
Binary_t< T >::operator T ( ) const
inline

Definition at line 146 of file Binary_t.h.

◆ operator!=() [1/2]

template<class T >
Bool_t Binary_t< T >::operator!= ( const Binary_t< T > &  b2)

Test inequality

Definition at line 367 of file Binary_t.h.

◆ operator!=() [2/2]

template<class T >
Bool_t Binary_t< T >::operator!= ( const Char_t b2)

Test inequality

Definition at line 373 of file Binary_t.h.

◆ operator&()

template<class T >
Binary_t< T > Binary_t< T >::operator& ( const Binary_t< T > &  b1)

Binary_t operator|(const Long64_t l1); Binary_t operator|(const Char_t* c1); Binary_t operator|(const Hexa_t& h1); //and

Binary_t Binary_t<T>::operator|(const Hexa_t& b1) { //bitwise OR of two binary numbers return Binary_t(b1.Value() | fVal); }

Binary_t Binary_t<T>::operator|(const T l1) { //bitwise OR of two binary numbers return Binary_t(fVal | l1); }

Binary_t operator|(const T l1, const Binary_t& b2) { //bitwise OR of two binary numbers return (b2|I1); }

Binary_t operator|(const Char_t* c1) { //bitwise OR of two binary numbers return (Binary_t(c1)|(*this)); }

Binary_t operator|(const Char_t* c1, const Binary_t& b2) { //bitwise OR of two binary numbers Binary_t b1(c1); Binary_t tmp(b1.Value() | b2.Value()); return tmp; }

//-----------------------------------------------—bitwise AND operators

bitwise AND of two binary numbers

Definition at line 461 of file Binary_t.h.

◆ operator=() [1/3]

template<class T >
Binary_t< T > & Binary_t< T >::operator= ( const Binary_t< T > &  val)

equals operators

Binary_t& Binary_t<T>::operator=(const Hexa_t& val) { //Assign a hexadecimal value to the binary number Set(val.Value()); return (*this); }

Assign a value to the binary number

Definition at line 354 of file Binary_t.h.

◆ operator=() [2/3]

template<class T >
Binary_t< T > & Binary_t< T >::operator= ( const Char_t val)

Assign a binary value (as a string) to the binary number

Definition at line 339 of file Binary_t.h.

◆ operator=() [3/3]

template<class T >
Binary_t< T > & Binary_t< T >::operator= ( const T  val)

Assign a decimal value to the binary number

Definition at line 331 of file Binary_t.h.

◆ operator==()

template<class T >
Bool_t Binary_t< T >::operator== ( const Binary_t< T > &  b2)

Binary_t operator&(const Long64_t l1); Binary_t operator&(const Char_t* c1); Binary_t operator&(const Hexa_t& h1);

Test equality

Definition at line 361 of file Binary_t.h.

◆ operator|()

template<class T >
Binary_t< T > Binary_t< T >::operator| ( const Binary_t< T > &  b1)

-----------------------------------------------—bitwise OR operators

addtion Binary_t<T> operator+(const Binary_t<T>& b1); Binary_t<T> operator+(const T l1); Binary_t<T> operator+(const Char_t* c1); Binary_t<T> operator+(const Hexa_t& h1); or

-----------------------------------------------—addition operators

template <class T> Binary_t<T> Binary_t<T>::operator+(const Binary_t<T>& b1) { //Addition of two binary numbers return Binary_t(fVal + b1.Value()); }

// Binary_t Binary_t<T>::operator+(const Hexa_t& h1) // { // //Addition of binary and hexa numbers // return Binary_t(fVal + h1.Value()); // }

template <class T> Binary_t<T> Binary_t<T>::operator+(const T I1) { //Addition of two binary numbers return Binary_t(fVal+I1); }

// Binary_t operator+(const T l1, const Binary_t& b2) // { // //Addition of two binary numbers // return (b2+l1); // }

template <class T> Binary_t<T> Binary_t<T>::operator+(const Char_t* c1) { //Addition of two binary numbers return ((*this)+Binary_t<T>(c1)); }

Binary_t operator+(const Char_t* c1, const Binary_t& b2) { //Addition of two binary numbers return (b2+c1); }

bitwise OR of two binary numbers

Definition at line 420 of file Binary_t.h.

◆ Print()

template<class T >
void Binary_t< T >::Print ( Option_t opt = "") const

Definition at line 576 of file Binary_t.h.

◆ ResetBit()

template<class T >
void Binary_t< T >::ResetBit ( UChar_t  nbit)

Set bit 'nbit' to 0 The least significant bit is numbered 0.

Definition at line 516 of file Binary_t.h.

◆ Set() [1/2]

template<class T >
void Binary_t< T >::Set ( const Char_t val)

Set value from string containing binary number If their are more bits in string than number can hold, we only use the fNBits bits from bit 0 (LSB) to bit fNBits-1

Definition at line 264 of file Binary_t.h.

◆ Set() [2/2]

template<class T >
void Binary_t< T >::Set ( const T  val)

Set binary number corresponding to decimal value "val"

Definition at line 252 of file Binary_t.h.

◆ SetBit() [1/2]

template<class T >
void Binary_t< T >::SetBit ( UChar_t  nbit)

Binary_t operator&(const Binary_t& b1, const T l2) { //bitwise AND of two binary numbers Binary_t tmp(b1.Value() & l2); return tmp; }

Binary_t operator&(const T l1, const Binary_t& b2) { //bitwise AND of two binary numbers Binary_t tmp(l1 & b2.Value()); return tmp; }

Binary_t operator&(const Binary_t& b1, const Char_t* c2) { //bitwise AND of two binary numbers Binary_t b2(c2); Binary_t tmp(b1.Value() & b2.Value()); return tmp; }

Binary_t operator&(const Char_t* c1, const Binary_t& b2) { //bitwise AND of two binary numbers Binary_t b1(c1); Binary_t tmp(b1.Value() & b2.Value()); return tmp; }

Set bit 'nbit' to 1 The least significant bit is numbered 0.

Definition at line 498 of file Binary_t.h.

◆ SetBit() [2/2]

template<class T >
void Binary_t< T >::SetBit ( UChar_t  nbit,
T  val 
)

Set bit 'nbit' to 1 to the same value as the equivalent bit in 'val'

Definition at line 506 of file Binary_t.h.

◆ SetNBits()

template<class T >
void Binary_t< T >::SetNBits ( UChar_t  nbits)
inline

Definition at line 165 of file Binary_t.h.

◆ String()

template<class T >
const Char_t * Binary_t< T >::String ( UChar_t  nbits = 0)

Return string containing binary representation of number By default, shortest possible representation is returned. If a default number of bits has been set with SetNBits, or if the number of bits is given as argument, the number will be represented using that many bits. If this is more than are necessary, we pad with leading zeroes. If there are not enough bits to represent the whole of the number, only the bits from 0 to (nbits-1) are shown.

e.g. if we have Binary_t b="111011" : b.String(8) will give "00111011" (padded with 2 leading zeroes) b.String(3) will give "011" (bits 0 to 2 only, i.e. first 3 bits starting from bit 0, which is LSB)

work out number of leading zeroes to add (if any) if number is too short for required number of bits, lzos>0 and equals number of zeroes to pad with if number is too long for required number of bits, lzos<0

Add 'lzos' leading zeroes

cut off (-lzos) most significant bits

Definition at line 283 of file Binary_t.h.

◆ Subvalue()

template<class T >
T Binary_t< T >::Subvalue ( UChar_t  msb,
UChar_t  nbits 
)

Returns the decimal value of a part of this binary number. The bits (firstbit, firstbit-1, firstbit-2, ... , firstbit-nbits+1) [see BIT NUMBERING CONVENTION] are used. If firstbit-nbits+1 < 0 (i.e. there are not enough bits in the number) we use all the bits from firstbit down to bit 0.

transform all to 'int' for arithmetic to work!!

check number of bits

Get string representation of value with enough bits i.e. (firstbit+1) bits

Now get value of the required substring of this string

Definition at line 583 of file Binary_t.h.

◆ TestBit()

template<class T >
Bool_t Binary_t< T >::TestBit ( UChar_t  nbit)

Returns kTRUE if bit is equal to '1' The least significant bit is numbered 0.

Definition at line 524 of file Binary_t.h.

◆ Value()

template<class T >
T Binary_t< T >::Value

Get unsigned decimal value of binary number

Definition at line 258 of file Binary_t.h.

◆ WriteSubvalue()

template<class T >
void Binary_t< T >::WriteSubvalue ( const T  val,
UChar_t  msb,
UChar_t  nbits 
)

Write the value 'val' in the 'nbits' bits from bit 'msb' (Most Significant Bit) to bit (msb-nbits+1). Warning if the following conditions are not respected: 1 <= nbits <= fNBits 0 <= msb <= (fNBits-1) (msb-nbits+1)>=0

transform everything to 'int' for arithmetic to work normally!

get binary representation of 'val' with nbits

get full fNBits bit representation

replace nbits bits from bit msb to msb-nbits+1 with binary representation of 'val'

set new value

Definition at line 538 of file Binary_t.h.

Member Data Documentation

◆ fDumString

template<class T >
TString Binary_t< T >::fDumString
private

Definition at line 109 of file Binary_t.h.

◆ fDumString2

template<class T >
TString Binary_t< T >::fDumString2
private

dummy, used by String method

Definition at line 110 of file Binary_t.h.

◆ fNBits

template<class T >
UChar_t Binary_t< T >::fNBits
private

Definition at line 106 of file Binary_t.h.

◆ fNBitsRep

template<class T >
UChar_t Binary_t< T >::fNBitsRep
private

Definition at line 105 of file Binary_t.h.

◆ fVal

template<class T >
T Binary_t< T >::fVal
private

Definition at line 107 of file Binary_t.h.


The documentation for this class was generated from the following file: