BitField Class Reference
<class description="" goes="" here=""> <short description="">
More...
#include <BitField.hpp>
List of all members.
|
Public Member Functions |
|
| BitField (char *data, u32 size) |
|
| BitField (u32 size, bool initValue) |
| void | setBit (u32 pos) |
| | sets the bit at position
|
| void | clearBit (u32 pos) |
| | sets the bit at position
|
| bool | getBit (u32 pos) const |
| | gets the bit at position
|
|
void | invert () |
| | inverts all 1 to 0 and all 0 to 1
|
| u32 | getBitRange (u32 startPos, int *nrBits, bool littleEndian=true) |
| | returns
|
|
bool | writeBitRange (u32 startPos, int nrBits, u32 value, u32 startBit, bool littleEndian=true) |
|
bool | isAllSet () const |
| | returns true if the bitfield consists completely of 1s
|
|
bool | isAllCleared () const |
| | returns true if the bitfield consists completely of 0s
|
| float | getPercentageSetBits () const |
| | returns the percentage of 1s inthe bitfield.
|
| BitField * | createWithNewSize (u32 size, bool initValueForMissing) |
| | creates a new BitField with new size
|
|
u32 | getSize () const |
| const char * | toBinaryString (u32 *sizeInU32) const |
| | simply returns a pointer to the internal bitfield.
|
| char * | toAsciiString () const |
| | converts the bitfield into a human viewer friendly 01 ASCII presentation.
|
| char * | toHexString () const |
| | converts the bitfield into a hex representation of the form size_0123456789ABCDEF...
|
Static Public Member Functions |
| BitField * | fromAsciiString (const char *str) |
| | creates a Bitfield from a string of chars '0' and '1'.
|
| BitField * | fromBinaryString (const char *bin, u32 numBits) |
| | creates a Bitfield from a binary data pointer.
|
|
BitField * | fromBinaryString (char *bin, u32 numBits) |
| | same as above but faster because only a shallow-copy is performed
|
|
BitField * | fromHexString (const char *bin) |
| | creates a bitfield from its hex presentation
|
|
void | doTest () |
| | automatic testing of BitField functionality, throws assertion at any error found
|
Detailed Description
<class description="" goes="" here=""> <short description="">
- Author:
- Michael Kropfberger and Peter Schojer
- Version:
- Id
- BitField.hpp,v 1.4 2004/05/19 22:19:01 mkropfbe Exp
Definition at line 62 of file BitField.hpp.
Member Function Documentation
| void BitField::clearBit |
( |
u32 |
pos |
) |
|
|
|
|
sets the bit at position
- Parameters:
-
| pos | to 0. if pos points to an invalid bit, the clear is ignored |
Definition at line 75 of file BitField.cpp.
Referenced by createWithNewSize(), fromAsciiString(), fromHexString(), and invert(). 00076 {
00077 if(pos>=maxSize)
00078 return;
00079
00080 u32 posInArray=pos/(sizeof(u32)*8);
00081 u32 bitInPos=pos%(sizeof(u32)*8);
00082 bits[posInArray]&=(((u32)0xffffffff)-(1<<bitInPos));
00083 };
|
| BitField * BitField::createWithNewSize |
( |
u32 |
size, |
|
|
bool |
initValueForMissing |
|
) |
|
|
|
|
creates a new BitField with new size
- Parameters:
-
| size. | If the new size is smaller than the current size, the old values are copied from pos 0 to newSize. if the new size is the same than the current size, this method has clone functionality. if the new size is larger than the current size, the current data is cloned and the values afterwards are set to |
| initValueForMissing | |
Definition at line 126 of file BitField.cpp.
References bits, clearBit(), getBit(), and setBit().
Referenced by ESInfo::setVOPTimeIncrement(). 00127 {
00128
00129 BitField* b= new BitField(size,initValueForMissing);
00130
00131
00132 u32 posInArray=size/( sizeof(u32)*8);
00133 if(size>maxSize)
00134 posInArray=maxSize/( sizeof(u32)*8);
00135
00136 memcpy(b-> bits,bits,posInArray* sizeof(u32));
00137
00138
00139 u32 startPosRemaining=posInArray*( sizeof(u32)*8);
00140 u32 endPos=MIN(size,maxSize);
00141 for(u32 i=startPosRemaining;i<endPos;i++) {
00142 if( getBit(i))
00143 b-> setBit(i);
00144 else
00145 b-> clearBit(i);
00146 }
00147 return b;
00148 };
|
| BitField * BitField::fromAsciiString |
( |
const char * |
str |
) |
[static] |
|
|
|
creates a Bitfield from a string of chars '0' and '1'.
Time expensive but endian save! Definition at line 224 of file BitField.cpp.
References clearBit().
Referenced by doTest(). 00225 {
00226 if(str==NULL)
00227 return NULL;
00228
00229 u32 sizeStr=strlen(str);
00230 BitField* b= new BitField(sizeStr, true);
00231
00232
00233
00234 for(u32 i=0;i<sizeStr;i++) {
00235 if(str[i]== '0')
00236 b-> clearBit(i);
00237 }
00238 return b;
00239 };
|
| BitField * BitField::fromBinaryString |
( |
const char * |
bin, |
|
|
u32 |
numBits |
|
) |
[static] |
|
|
|
creates a Bitfield from a binary data pointer.
fast but not endian save Definition at line 243 of file BitField.cpp.
References bits. 00243 {
00244 BitField* b= new BitField(numBits, false);
00245 u32 bytes=(numBits+ sizeof( char)-1)/ sizeof( char);
00246 memcpy(b-> bits,bin,bytes);
00247 return b;
00248 };
|
| bool BitField::getBit |
( |
u32 |
pos |
) |
const |
|
|
|
gets the bit at position
- Parameters:
-
| pos. | if pos points to an invalid bit, always false is returned |
Definition at line 95 of file BitField.cpp.
Referenced by createWithNewSize(), doTest(), getBitRange(), FilteredIO::getFrame(), getPercentageSetBits(), invert(), isAllCleared(), isAllSet(), toAsciiString(), toHexString(), and FilteredIO::writeFrame(). 00096 {
00097 if(pos>=maxSize)
00098 return false;
00099
00100 u32 posInArray=pos/(sizeof(u32)*8);
00101 u32 bitInPos=pos%(sizeof(u32)*8);
00102 return bits[posInArray]&(1<<bitInPos);
00103 };
|
| u32 BitField::getBitRange |
( |
u32 |
startPos, |
|
|
int * |
nrBits, |
|
|
bool |
littleEndian = true |
|
) |
|
|
|
|
returns
- Parameters:
-
| nrBits | from the bitfield, starting with bitPos |
| startPos. | if nrBits is an invalid value (0 or greater 32) or if the specified bit range exceeds the size of the bitfield, |
| nrBits | is set to -1. |
| littleEndian | specifies endianness. When set to true the decimal value for the requested bitrange is calculated as if the first bit has the lowest value. e.g.: Bit 0 1000 Bit 3 -> littleEndian=true --> 1 littleEndian=false --> 8 If everything went fine nrBits returns the number of bits used to create |
| return,otherwise | |
| return | is 0 and |
| nrBits | is -1 |
Definition at line 346 of file BitField.cpp.
References getBit().
Referenced by doTest(). 00347 {
00348 assert(nrBits);
00349 if( (*nrBits)==0 || (*nrBits)>32 || startPos+(*nrBits)>maxSize) {
00350 (*nrBits)=-1;
00351 return 0;
00352 }
00353
00354 u32 result=0;
00355 u32 shamt=0;
00356 u32 i=0;
00357 if(!littleEndian) {
00358 shamt=(*nrBits)-1;
00359 }
00360 for (i=startPos;i<startPos+(*nrBits);i++) {
00361 result+=( getBit(i)?1:0)<<shamt;
00362 if(littleEndian)
00363 shamt++;
00364 else
00365 shamt--;
00366 }
00367
00368 return result;
00369 };
|
| float BitField::getPercentageSetBits |
( |
|
) |
const |
|
|
|
returns the percentage of 1s inthe bitfield.
Expensive!!! Definition at line 209 of file BitField.cpp.
References getBit().
Referenced by doTest(), and ProxySession::tearDown(). 00210 {
00211 float res=0;
00212 int count=0;
00213 for(u32 i=0;i<maxSize;i++) {
00214 if( getBit(i))
00215 count++;
00216 }
00217 if(count>0)
00218 res= ( float)(( double)count)/(( double)maxSize);
00219 return res;
00220 };
|
| void BitField::setBit |
( |
u32 |
pos |
) |
|
|
| char * BitField::toAsciiString |
( |
|
) |
const |
|
|
|
converts the bitfield into a human viewer friendly 01 ASCII presentation.
Time expensive but endian save! Definition at line 160 of file BitField.cpp.
References getBit().
Referenced by doTest(). 00161 {
00162 char* temp= new char[maxSize+1];
00163 for(u32 i=0;i<maxSize;i++)
00164 temp[i]=( getBit(i) ? '1' : '0');
00165
00166 temp[maxSize]= '\0';
00167 return temp;
00168 };
|
| const char * BitField::toBinaryString |
( |
u32 * |
sizeInU32 |
) |
const |
|
|
|
simply returns a pointer to the internal bitfield.
returns the size which is multiple of sizeof(u32) Definition at line 152 of file BitField.cpp. 00153 {
00154 *sizeInU32=(maxSize+(sizeof(u32)*8-1))/(sizeof(u32)*8);
00155 return (const char*)bits;
00156 };
|
| char * BitField::toHexString |
( |
|
) |
const |
|
|
|
converts the bitfield into a hex representation of the form size_0123456789ABCDEF...
Example: A Bitfield with the size 9, data:(bit 0) 000111110 (bit 8) is in hex: 00000009_8F0Definition at line 261 of file BitField.cpp.
References getBit().
Referenced by doTest(), and FilteredIO::setBitField(). 00262 {
00263
00264 int size=(maxSize+3)/4;
00265
00266 size+=9;
00267 char* result= new char[size+1];
00268
00269 sprintf(result, "%8X_",maxSize);
00270 int cnt=0;
00271
00272 for(u32 i=0;i<maxSize;i+=4) {
00273 cnt=( getBit(i) ? 1 : 0);
00274 cnt+=( getBit(i+1) ? 1 : 0)<<1;
00275 cnt+=( getBit(i+2) ? 1 : 0)<<2;
00276 cnt+=( getBit(i+3) ? 1 : 0)<<3;
00277 if(cnt>9) {
00278 cnt-=10;
00279 cnt+= 'A';
00280 }
00281 else
00282 cnt+= '0';
00283 result[9+i/4]=cnt;
00284 }
00285 result[size]= '\0';
00286 return result;
00287 };
|
The documentation for this class was generated from the following files: