24 uchar BitString::bitMasks[8] = { 1, 2, 4, 8, 16, 32, 64, 128};
26 uchar BitString::bitMasksRev[8] = {255-1, 255-2, 255-4, 255-8, 255-16, 255-32, 255-64, 255-128};
40 bitCounts =
new uchar[256];
43 for (byte = 0; byte < 256; byte++)
62 byteLen = ((bitLen - 1) / 8) + 1;
72 byteLen = ((bitLen - 1) / 8) + 1;
73 str =
new uchar[byteLen];
74 memset (str, 0, byteLen);
84 str =
new uchar[byteLen];
85 memcpy (str, bs.str, byteLen);
97 byteLen = ((bitLen - 1) / 8) + 1;
98 str =
new uchar[byteLen];
99 memset (str, 0, byteLen);
101 for (x = 0; x < bitNumsLen; x++)
103 if (bitNums[x] >= bitLen)
106 msg <<
"BitString Constructing from list of numbers: bitNums[" << x <<
"] = [" << bitNums[x] <<
"] which is >= bitLen[" << bitLen <<
"].";
107 cerr << std::endl << std::endl <<
"BitString ***ERROR*** " << msg << std::endl << std::endl;
136 byteOffset = bitNum / 8;
137 bitOffset = bitNum % 8;
149 for (byteOffset = 0; byteOffset < byteLen; byteOffset++)
150 count += bitCounts[str[byteOffset]];
163 CalcByteAndBitOffsets (bitNum, byteOffset, bitOffset);
165 bool bit = ((str[byteOffset] & bitMasks[bitOffset]) != 0);
175 memset (str, 255, byteLen);
184 if (bitNum >= bitLen)
188 <<
"BitString::Set Invalid Index[" << bitNum <<
"] BitString::bitLen[" << bitLen <<
"]." << std::endl
196 CalcByteAndBitOffsets (bitNum, byteOffset, bitOffset);
198 uchar& br = str[byteOffset];
200 br = (br | bitMasks[bitOffset]);
209 memset (str, 0, byteLen);
217 if (bitNum >= bitLen)
221 <<
"BitString::Set Invalid Index[" << bitNum <<
"] BitString::bitLen[" << bitLen <<
"]." << std::endl
229 CalcByteAndBitOffsets (bitNum, byteOffset, bitOffset);
231 uchar& br = str[byteOffset];
233 br = (br & bitMasksRev[bitOffset]);
241 boolVector.erase (boolVector.begin (), boolVector.end ());
247 for (byteOffset = 0; byteOffset < byteLen; byteOffset++)
249 uchar br = str[byteOffset];
250 if (byteOffset < (byteLen - 1))
253 numOfBits = bitLen % 8;
255 for (x = 0; x < numOfBits; x++)
257 boolVector.push_back ((br % 2) == 1);
271 msg <<
"BitString::ListOfSetBits BitLen[" << bitLen <<
"] of this instance of BitString exceeds capacity of 'VectorUint16'.";
272 cerr << std::endl <<
"BitString::ListOfSetBits ***ERROR*** " << msg << std::endl << std::endl;
282 for (byteOffset = 0; byteOffset < byteLen; byteOffset++)
284 uchar br = str[byteOffset];
292 if (byteOffset < (byteLen - 1))
296 numOfBits = bitLen % 8;
302 for (x = 0; x < numOfBits; x++)
306 setBits.push_back ((kkuint16)bitNum);
325 for (byteOffset = 0; byteOffset < byteLen; byteOffset++)
327 uchar br = str[byteOffset];
335 if (byteOffset < (byteLen - 1))
339 numOfBits = bitLen % 8;
345 for (x = 0; x < numOfBits; x++)
349 setBits.push_back (bitNum);
365 static char hexChars[] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
371 for (byteOffset = 0; byteOffset < byteLen; byteOffset++)
373 high4Bits = str[byteOffset] / 16;
374 low4Bits = str[byteOffset] % 16;
387 if ((hexChar >=
'0') && (hexChar <=
'9'))
390 hexChar = (
char)toupper (hexChar);
391 if ((hexChar <
'A') || (hexChar >
'F'))
412 while (x < hexStrLen)
414 low4Bits = HexCharToInt (hexStr
[x]);
421 high4Bits = HexCharToInt (hexStr
[x]);
431 bs.str[byteNum] = (
uchar)low4Bits + (
uchar)high4Bits * 16;
445 bitLen = right.bitLen;
446 byteLen = right.byteLen;
447 str =
new uchar[byteLen];
450 for (x = 0; x < byteLen; x++)
451 str[x] = right.str[x];
461 kkuint32 shortestByteLen = Min (byteLen, right.byteLen);
465 for (x = 0; x < shortestByteLen; x++)
467 str[x] = str[x] | right.str[x];
486 kkuint32 shortestByteLen = Min (byteLen, right.byteLen);
490 for (x = 0; x < shortestByteLen; x++)
492 str[x] = str[x] & right.str[x];
494 for (x = shortestByteLen; x < byteLen; ++x)
514 kkuint32 shortestByteLen = Min (byteLen, right.byteLen);
518 while (x < shortestByteLen)
520 if (str[x] < right.str[x])
523 else if (str[x] > right.str[x])
532 if (x >= right.byteLen)
547 return (Compare (right) == 0);
554 return (Compare (right) != 0);
561 return (Compare (right) > 0);
568 return (Compare (right) >= 0);
575 return (Compare (right) < 0);
582 kkuint32 shortestByteLen = Min (byteLen, right.byteLen);
583 kkuint32 longestByteLen = Max (byteLen, right.byteLen);
587 for (x = 0; x < shortestByteLen; x++)
588 str[x] = str[x] ^ right.str[x];
590 for (x = shortestByteLen; x < longestByteLen; x++)
600 kkuint32 shortestByteLen = Min (byteLen, right.byteLen);
601 kkuint32 longestByteLen = Max (byteLen, right.byteLen);
605 BitString result (Max (bitLen, right.bitLen));
607 for (x = 0; x < shortestByteLen; x++)
608 result.str[x] = str[x] ^ right.str[x];
610 for (x = shortestByteLen; x < longestByteLen; x++)
626 while (t && (!cancelFlag))
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
XmlTag(const KKStr &_name, TagTypes _tagType)
kkuint32 Count() const
Returns number of bits set to '1'.
BitString & operator&=(const BitString &right)
Performs a bitwise AND against the left operand.
std::vector< kkuint32 > VectorUint32
Vector of unsigned 32 bit integers.
void ReSet(kkuint32 bitNum)
Set the bit indicated by 'bitNum' to '0'.
virtual BitString * Duplicate() const
static BitString FromHexStr(const KKStr &hexStr, bool &validHexStr)
Create a bit-string from a Hex String.
void Set()
Set all bits to '1'.
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
KKStr HexStr() const
Returns a Hex-String representation.
void ReSet()
Set all bits to '0'.
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
bool operator!=(const BitString &right) const
void PopulateVectorBool(VectorBool &boolVector) const
Populates a boolean vector where each element reflects whether the corresponding bit is set...
BitString & operator^=(const BitString &right)
Performs a bitwise XOR against the left operand.
unsigned __int16 kkuint16
16 bit unsigned integer.
XmlContent * XmlContentPtr
bool operator>(const BitString &right) const
BitString & operator|=(const BitString &right)
Performs a bitwise OR against the left operand.
BitString operator^(const BitString &right)
Performs a bitwise XOR between two operands returning a new BitString.
BitString & operator+=(const BitString &right)
Performs a bitwise OR against the left operand.
bool Test(kkuint32 bitNum) const
unsigned __int32 kkuint32
std::vector< bool > VectorBool
bool operator==(const BitString &right) const
Allows you to manage very long bit strings.
kkuint32 Len() const
Returns the number of characters in the string.
BitString(const BitString &b)
Copy constructor.
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
BitString & operator=(const BitString &right)
unsigned char uchar
Unsigned character.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
void ListOfSetBits16(VectorUint16 &setBits) const
void Set(kkuint32 bitNum)
Set the bit indicated by 'bitNum' to '1'.
BitString()
Instantiates a empty bit-string of length 0; needed for ReadXML.
BitString & operator*=(const BitString &right)
Performs a bitwise AND against the left operand.
static void WriteXml(const KKStr &s, std::ostream &o)
BitString(kkuint32 _bitLen, kkuint16 *_bitNums, kkuint32 _bitNumsLen)
Construct a BitString of length _bitLen with bits indicated by '_bitNums' set to '1'.
char operator[](kkint32 i) const
void WriteXML(std::ostream &o)
KKStrPtr const Content() const
bool operator<(const BitString &right) const
Used for logging messages.
BitString(kkuint32 _bitLen)
Construct a bit string of length _binLen with all bits set to '0'.
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
KKException(const KKStr &_exceptionStr)
std::vector< kkuint16 > VectorUint16
Vector of unsigned 16 bit integers.
void ListOfSetBits32(VectorUint32 &setBits) const
bool operator>=(const BitString &right) const
#define XmlFactoryMacro(NameOfClass)
volatile const bool VolConstBool