KaliVeda
1.13/01
Heavy-Ion Analysis Toolkit
|
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:
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()).
It is the same as the ROOT macro BIT(i) i.e.
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'
Set bit 'nbit' to 0 (the least significant bit is numbered 0).
Returns kTRUE if bit 'nbit' is equal to '1' (the least significant bit is numbered 0).
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:
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.
Returns maximum value that can be stored in number, i.e. when all bits = 1
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.
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>
Default constructor
Definition at line 220 of file Binary_t.h.
Create binary number corresponding to decimal value "val"
Definition at line 236 of file Binary_t.h.
Create binary number from string containing binary value
Definition at line 244 of file Binary_t.h.
copy constructor
Definition at line 228 of file Binary_t.h.
Definition at line 119 of file Binary_t.h.
Definition at line 169 of file Binary_t.h.
Returns number in its hexadecimal form
Definition at line 532 of file Binary_t.h.
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.
Returns maximum value that can be stored in number, i.e. when all bits = 1
Definition at line 609 of file Binary_t.h.
Definition at line 158 of file Binary_t.h.
Definition at line 154 of file Binary_t.h.
Definition at line 150 of file Binary_t.h.
Binary_t<T>& operator=(const Hexa_t&); convertors
Definition at line 142 of file Binary_t.h.
Definition at line 146 of file Binary_t.h.
Test inequality
Definition at line 367 of file Binary_t.h.
Test inequality
Definition at line 373 of file Binary_t.h.
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.
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.
Assign a binary value (as a string) to the binary number
Definition at line 339 of file Binary_t.h.
Assign a decimal value to the binary number
Definition at line 331 of file Binary_t.h.
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.
-----------------------------------------------—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.
Definition at line 576 of file Binary_t.h.
Set bit 'nbit' to 0 The least significant bit is numbered 0.
Definition at line 516 of file Binary_t.h.
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 binary number corresponding to decimal value "val"
Definition at line 252 of file Binary_t.h.
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.
Set bit 'nbit' to 1 to the same value as the equivalent bit in 'val'
Definition at line 506 of file Binary_t.h.
Definition at line 165 of file Binary_t.h.
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.
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.
Returns kTRUE if bit is equal to '1' The least significant bit is numbered 0.
Definition at line 524 of file Binary_t.h.
Get unsigned decimal value of binary number
Definition at line 258 of file Binary_t.h.
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.
Definition at line 109 of file Binary_t.h.
dummy, used by String method
Definition at line 110 of file Binary_t.h.
Definition at line 106 of file Binary_t.h.
Definition at line 105 of file Binary_t.h.
Definition at line 107 of file Binary_t.h.