1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This is a service class for packing and unpacking bits in a 32 bit word.
22 ///////////////////////////////////////////////////////////////////////////////
26 #include "AliBitPacking.h"
29 ClassImp(AliBitPacking)
32 //_____________________________________________________________________________
33 Bool_t AliBitPacking::PackWord(UInt_t data, UInt_t &word,
34 Int_t startBit, Int_t stopBit)
36 // Packs data into the word buffer from startBit bit up to stopBit bit
38 // check that startBit and stopBit are reasonable
39 if (startBit > stopBit) {
40 ::Error("AliBitPacking::PackWord",
41 "startBit is larger than stopBit");
45 ::Error("AliBitPacking::PackWord",
46 "stopBit exceeds valid range of 32 bits");
50 // create a word with the bits 0 to (stopBit-startBit) set
51 UInt_t bits = 0xFFFFFFFF;
52 if (stopBit-startBit < 31) bits = (1 << (stopBit-startBit+1)) - 1;
54 // check that the data fits into the given bit range
56 ::Error("AliBitPacking::PackWord",
57 "Word to be filled is not within desired length");
61 // clear the bits from startBit to stopBit
62 word &= (0xFFFFFFFF ^ (bits << startBit));
64 // fill in the data bits
65 word |= (data << startBit);
70 //_____________________________________________________________________________
71 UInt_t AliBitPacking::UnpackWord(UInt_t word, Int_t startBit, Int_t stopBit)
73 // Unpacks data of stopBit-startBit+1 bits from word buffer starting from
74 // the position indicated by startBit
76 // check that startBit and stopBit are reasonable
77 if (startBit > stopBit) {
78 ::Error("AliBitPacking::UnpackWord",
79 "startBit is larger than stopBit");
83 ::Error("AliBitPacking::UnpackWord",
84 "stopBit exceeds valid range of 32 bits");
88 // create a word with the bits 0 to (stopBit-startBit) set
89 UInt_t bits = 0xFFFFFFFF;
90 if (stopBit-startBit < 31) bits = (1 << (stopBit-startBit+1)) - 1;
92 // pick out the requested bits
93 return ((word >> startBit) & bits);