ROOT logo
#include "hbitman.h"

#include <stdlib.h>
#include  <iostream>
using namespace std;

//_HADES_CLASS_DESCRIPTION
///////////////////////////////////////////////////////////
// HBitMan
//
// library of static functions
// look here for auxiliary functions for:
// bit manipulations.
//
// The direct bit functions can be used static.
// Bit counting starts at 0. 0 represents the lowest bit.
// The function do not check the range! Take care you
// stay inside the range.
// For the bit functions using names see the following
// example:
//
//  bits in Integer:
//  11
//  10       tofrec     (no sign)
//  9
//  8
//  7.       charge -1 0 1 (sign)
//  6.
//  5.       system 0-3 (no sign)
//  4........
//  3........sector 0-7 (no sign)
//  2........
//  1........
//
// // allocate the word map. Bit are allocated
// // in the order of declaration. word width include the
// // extra bit for sign if sign is specified!
// HBitMan bitman;
// bitman.createWord("sector",3,kFALSE);
// bitman.createWord("sys"   ,3,kFALSE);
// bitman.createWord("charge",3,kTRUE);  // 2bits+sign = 3
// bitman.createWord("tofrec",3,kFALSE);
// bitman.init();
//
//
//
// UInt_t common = 0;               // flag integer
//
// // set values
// bitman.setWord(common,"sector",3);
// bitman.setWord(common,"sys"   ,1);
// bitman.setWord(common,"charge",-1);
// bitman.setWord(common,"tofrec",3);
//
//
// bitman.printBits(common); // print binary representation
// cout<<bitman.getWord(common,"sector")<<endl;
// cout<<bitman.getWord(common,"sys")   <<endl;
// cout<<bitman.getWord(common,"charge")<<endl;
// cout<<bitman.getWord(common,"tofrec")<<endl;
// bitman.unsetWord(common,"sector");  // set bits for this word to 0
// bitman.printBits(common);
//
///////////////////////////////////////////////////////////

ClassImp(HBitMan)


    HBitMan::HBitMan()
{
}

HBitMan::~HBitMan()
{

}
//-----------------------------------------------------------
// direct bit setting
void HBitMan::printBits(UInt_t data){
    // print the binary reprentation of data.
    // Lowest bits at the right side
    for(Int_t i = 31; i >= 0; i --){ cout<< ((data >> i) & 0x1) <<flush; }
    cout<<endl;
}
void   HBitMan::setBit   (UInt_t &data,Int_t bit)  { data |=  ( 0x01 << bit ); }
void   HBitMan::unsetBit (UInt_t &data,Int_t bit)  { data &= ~( 0x01 << bit ); }
Bool_t HBitMan::getBit   (UInt_t &data,Int_t bit)  { return (data >> bit ) & 0x01 ; }

void HBitMan::setWord(UInt_t &data,Int_t num,Int_t width, Int_t val,Bool_t sign){
    // add data starting at bit num (from low) with n bits width
    // width includes the sign bit if sign is kTRUE.
    if(sign){
	data  = ( ((abs(val) & ((0x1<<(width-1))-1)) << num) | data ); //  first set word without sign
	data |= ( ( (val < 0) & 0x01) << (num+width) );                //  sign: highest bit of word=1 for negative
    } else {
	data  = ( ((val& ((0x1<<(width))-1)) << num) | data );
    }
}
void HBitMan::unsetWord(UInt_t &data,Int_t num,Int_t width){
    // unset data starting at bit num (from low) with n bits width
    data  =   ~((((0x1<<width)-1))<<num)  & data;
}
Int_t HBitMan::getWord(UInt_t &data,Int_t num,Int_t width,Bool_t sign){
    if(sign){
	if( ((data >> (num+width) ) & 0x01) == 1){
	    return  -((data >> num ) & ((0x1<<(width-1))-1));
	} else {
	    return ((data >> num ) & ((0x1<<(width-1))-1));
	}
    } else {
	return (data >> num ) & ((0x1<<width)-1);
    }
}
//-----------------------------------------------------------





//-----------------------------------------------------------
// bit setting using names
void HBitMan::createWord(const TString name,Int_t width, Bool_t sign){
    // bit setting using names
    word w;
    w.name  = name;
    w.width = width;
    w.sign  = sign;
    list.push_back(w);
}

void HBitMan::init(){
    // bit setting using names needs init after
    // defining all word to complete the catalog
    Int_t off = 0;

    for(UInt_t i = 0; i < list.size(); i ++){
	word& w  = list[i];
	w.offset = off;
	off     += w.width;
	wordmap[list[i].name] = list[i];
    }
}

void HBitMan::setWord(UInt_t &data,const TString name,Int_t val){
    // set word with lable name to value val
    word& w = wordmap[name];
    setWord(data,w.offset,w.width,val,w.sign);
}
void HBitMan::unsetWord(UInt_t &data,const TString name){
    // unset word with lable name to value 0
    word& w = wordmap[name];
    unsetWord(data,w.offset,w.width);
}
Int_t HBitMan::getWord(UInt_t &data,const TString name){
    // retrieve word with lable name
    word& w = wordmap[name];
    return getWord(data,w.offset,w.width,w.sign);
}
//-----------------------------------------------------------
 hbitman.cc:1
 hbitman.cc:2
 hbitman.cc:3
 hbitman.cc:4
 hbitman.cc:5
 hbitman.cc:6
 hbitman.cc:7
 hbitman.cc:8
 hbitman.cc:9
 hbitman.cc:10
 hbitman.cc:11
 hbitman.cc:12
 hbitman.cc:13
 hbitman.cc:14
 hbitman.cc:15
 hbitman.cc:16
 hbitman.cc:17
 hbitman.cc:18
 hbitman.cc:19
 hbitman.cc:20
 hbitman.cc:21
 hbitman.cc:22
 hbitman.cc:23
 hbitman.cc:24
 hbitman.cc:25
 hbitman.cc:26
 hbitman.cc:27
 hbitman.cc:28
 hbitman.cc:29
 hbitman.cc:30
 hbitman.cc:31
 hbitman.cc:32
 hbitman.cc:33
 hbitman.cc:34
 hbitman.cc:35
 hbitman.cc:36
 hbitman.cc:37
 hbitman.cc:38
 hbitman.cc:39
 hbitman.cc:40
 hbitman.cc:41
 hbitman.cc:42
 hbitman.cc:43
 hbitman.cc:44
 hbitman.cc:45
 hbitman.cc:46
 hbitman.cc:47
 hbitman.cc:48
 hbitman.cc:49
 hbitman.cc:50
 hbitman.cc:51
 hbitman.cc:52
 hbitman.cc:53
 hbitman.cc:54
 hbitman.cc:55
 hbitman.cc:56
 hbitman.cc:57
 hbitman.cc:58
 hbitman.cc:59
 hbitman.cc:60
 hbitman.cc:61
 hbitman.cc:62
 hbitman.cc:63
 hbitman.cc:64
 hbitman.cc:65
 hbitman.cc:66
 hbitman.cc:67
 hbitman.cc:68
 hbitman.cc:69
 hbitman.cc:70
 hbitman.cc:71
 hbitman.cc:72
 hbitman.cc:73
 hbitman.cc:74
 hbitman.cc:75
 hbitman.cc:76
 hbitman.cc:77
 hbitman.cc:78
 hbitman.cc:79
 hbitman.cc:80
 hbitman.cc:81
 hbitman.cc:82
 hbitman.cc:83
 hbitman.cc:84
 hbitman.cc:85
 hbitman.cc:86
 hbitman.cc:87
 hbitman.cc:88
 hbitman.cc:89
 hbitman.cc:90
 hbitman.cc:91
 hbitman.cc:92
 hbitman.cc:93
 hbitman.cc:94
 hbitman.cc:95
 hbitman.cc:96
 hbitman.cc:97
 hbitman.cc:98
 hbitman.cc:99
 hbitman.cc:100
 hbitman.cc:101
 hbitman.cc:102
 hbitman.cc:103
 hbitman.cc:104
 hbitman.cc:105
 hbitman.cc:106
 hbitman.cc:107
 hbitman.cc:108
 hbitman.cc:109
 hbitman.cc:110
 hbitman.cc:111
 hbitman.cc:112
 hbitman.cc:113
 hbitman.cc:114
 hbitman.cc:115
 hbitman.cc:116
 hbitman.cc:117
 hbitman.cc:118
 hbitman.cc:119
 hbitman.cc:120
 hbitman.cc:121
 hbitman.cc:122
 hbitman.cc:123
 hbitman.cc:124
 hbitman.cc:125
 hbitman.cc:126
 hbitman.cc:127
 hbitman.cc:128
 hbitman.cc:129
 hbitman.cc:130
 hbitman.cc:131
 hbitman.cc:132
 hbitman.cc:133
 hbitman.cc:134
 hbitman.cc:135
 hbitman.cc:136
 hbitman.cc:137
 hbitman.cc:138
 hbitman.cc:139
 hbitman.cc:140
 hbitman.cc:141
 hbitman.cc:142
 hbitman.cc:143
 hbitman.cc:144
 hbitman.cc:145
 hbitman.cc:146
 hbitman.cc:147
 hbitman.cc:148
 hbitman.cc:149
 hbitman.cc:150
 hbitman.cc:151
 hbitman.cc:152
 hbitman.cc:153
 hbitman.cc:154
 hbitman.cc:155
 hbitman.cc:156
 hbitman.cc:157
 hbitman.cc:158
 hbitman.cc:159