diff options
Diffstat (limited to 'src/unstable/bitstring.cpp')
-rw-r--r-- | src/unstable/bitstring.cpp | 660 |
1 files changed, 330 insertions, 330 deletions
diff --git a/src/unstable/bitstring.cpp b/src/unstable/bitstring.cpp index 9559aad..3f24ec7 100644 --- a/src/unstable/bitstring.cpp +++ b/src/unstable/bitstring.cpp | |||
@@ -16,460 +16,460 @@ | |||
16 | 16 | ||
17 | Bu::BitString::BitString() | 17 | Bu::BitString::BitString() |
18 | { | 18 | { |
19 | caData = NULL; | 19 | caData = NULL; |
20 | cTopByteMask = 0; | 20 | cTopByteMask = 0; |
21 | iBits = iBytes = 0; | 21 | iBits = iBytes = 0; |
22 | } | 22 | } |
23 | 23 | ||
24 | Bu::BitString::BitString( const Bu::BitString &xSrc ) | 24 | Bu::BitString::BitString( const Bu::BitString &xSrc ) |
25 | { | 25 | { |
26 | iBits = xSrc.iBits; | 26 | iBits = xSrc.iBits; |
27 | iBytes = xSrc.iBytes; | 27 | iBytes = xSrc.iBytes; |
28 | cTopByteMask = xSrc.cTopByteMask; | 28 | cTopByteMask = xSrc.cTopByteMask; |
29 | caData = new unsigned char[iBytes]; | 29 | caData = new unsigned char[iBytes]; |
30 | memcpy( caData, xSrc.caData, iBytes ); | 30 | memcpy( caData, xSrc.caData, iBytes ); |
31 | 31 | ||
32 | fixup(); | 32 | fixup(); |
33 | } | 33 | } |
34 | 34 | ||
35 | Bu::BitString::BitString( long iNewBits, bool bFillRandomly ) | 35 | Bu::BitString::BitString( long iNewBits, bool bFillRandomly ) |
36 | { | 36 | { |
37 | long j; | 37 | long j; |
38 | iBits = iNewBits; | 38 | iBits = iNewBits; |
39 | iBytes = bitsToBytes( iNewBits );//(iNewBits/8)+((iNewBits%8)?(1):(0)); | 39 | iBytes = bitsToBytes( iNewBits );//(iNewBits/8)+((iNewBits%8)?(1):(0)); |
40 | caData = new unsigned char[iBytes]; | 40 | caData = new unsigned char[iBytes]; |
41 | 41 | ||
42 | setMask(); | 42 | setMask(); |
43 | 43 | ||
44 | if( bFillRandomly ) | 44 | if( bFillRandomly ) |
45 | { | 45 | { |
46 | // rand() only returns a value up to RAND_MAX (0x7FFF on my system) so | 46 | // rand() only returns a value up to RAND_MAX (0x7FFF on my system) so |
47 | // I'll just use the low order byte) | 47 | // I'll just use the low order byte) |
48 | for( j = 0; j < iBytes; j++ ) | 48 | for( j = 0; j < iBytes; j++ ) |
49 | { | 49 | { |
50 | caData[j] = (unsigned char)(Bu::Random::rand() & 0xFF); | 50 | caData[j] = (unsigned char)(Bu::Random::rand() & 0xFF); |
51 | } | 51 | } |
52 | } | 52 | } |
53 | else | 53 | else |
54 | { | 54 | { |
55 | clear(); | 55 | clear(); |
56 | } | 56 | } |
57 | 57 | ||
58 | fixup(); | 58 | fixup(); |
59 | } | 59 | } |
60 | 60 | ||
61 | Bu::BitString::~BitString() | 61 | Bu::BitString::~BitString() |
62 | { | 62 | { |
63 | if( caData != NULL ) delete[] caData; | 63 | if( caData != NULL ) delete[] caData; |
64 | } | 64 | } |
65 | 65 | ||
66 | Bu::BitString &Bu::BitString::operator=( const Bu::BitString &xSrc ) | 66 | Bu::BitString &Bu::BitString::operator=( const Bu::BitString &xSrc ) |
67 | { | 67 | { |
68 | if( caData != NULL ) | 68 | if( caData != NULL ) |
69 | { | 69 | { |
70 | delete[] caData; | 70 | delete[] caData; |
71 | } | 71 | } |
72 | iBits = xSrc.iBits; | 72 | iBits = xSrc.iBits; |
73 | iBytes = xSrc.iBytes; | 73 | iBytes = xSrc.iBytes; |
74 | cTopByteMask = xSrc.cTopByteMask; | 74 | cTopByteMask = xSrc.cTopByteMask; |
75 | caData = new unsigned char[iBytes]; | 75 | caData = new unsigned char[iBytes]; |
76 | memcpy( caData, xSrc.caData, iBytes ); | 76 | memcpy( caData, xSrc.caData, iBytes ); |
77 | 77 | ||
78 | fixup(); | 78 | fixup(); |
79 | 79 | ||
80 | return *this; | 80 | return *this; |
81 | } | 81 | } |
82 | 82 | ||
83 | Bu::BitString Bu::BitString::operator~() | 83 | Bu::BitString Bu::BitString::operator~() |
84 | { | 84 | { |
85 | Bu::BitString xRet( *this ); | 85 | Bu::BitString xRet( *this ); |
86 | 86 | ||
87 | for( int j = 0; j < xRet.iBytes; j++ ) | 87 | for( int j = 0; j < xRet.iBytes; j++ ) |
88 | { | 88 | { |
89 | xRet.caData[j] = ~xRet.caData[j]; | 89 | xRet.caData[j] = ~xRet.caData[j]; |
90 | } | 90 | } |
91 | 91 | ||
92 | xRet.fixup(); | 92 | xRet.fixup(); |
93 | 93 | ||
94 | return xRet; | 94 | return xRet; |
95 | } | 95 | } |
96 | 96 | ||
97 | Bu::BitString Bu::BitString::operator<<( const long iAmt ) | 97 | Bu::BitString Bu::BitString::operator<<( const long iAmt ) |
98 | { | 98 | { |
99 | if( iAmt == 0 ) | 99 | if( iAmt == 0 ) |
100 | { | 100 | { |
101 | return (*this); | 101 | return (*this); |
102 | } | 102 | } |
103 | //int iByteShift = iAmt/8; | 103 | //int iByteShift = iAmt/8; |
104 | 104 | ||
105 | Bu::BitString xSub( getSize() ); | 105 | Bu::BitString xSub( getSize() ); |
106 | 106 | ||
107 | long shft = (iAmt%8); | 107 | long shft = (iAmt%8); |
108 | long base = (iAmt/8); | 108 | long base = (iAmt/8); |
109 | unsigned char lowmask=0; | 109 | unsigned char lowmask=0; |
110 | for( long j = 0; j < 8-shft; j++ ) | 110 | for( long j = 0; j < 8-shft; j++ ) |
111 | { | 111 | { |
112 | lowmask |= (1<<j); | 112 | lowmask |= (1<<j); |
113 | } | 113 | } |
114 | for( long j = 0; j < xSub.iBytes; j++ ) | 114 | for( long j = 0; j < xSub.iBytes; j++ ) |
115 | { | 115 | { |
116 | xSub.caData[base+j] = ((caData[j]>>shft)&(lowmask)) | ((caData[j+1]<<(8-shft))&(~lowmask)); | 116 | xSub.caData[base+j] = ((caData[j]>>shft)&(lowmask)) | ((caData[j+1]<<(8-shft))&(~lowmask)); |
117 | } | 117 | } |
118 | xSub.fixup(); | 118 | xSub.fixup(); |
119 | 119 | ||
120 | return xSub; | 120 | return xSub; |
121 | } | 121 | } |
122 | 122 | ||
123 | Bu::BitString Bu::BitString::operator>>( const long iAmt ) | 123 | Bu::BitString Bu::BitString::operator>>( const long iAmt ) |
124 | { | 124 | { |
125 | if( iAmt == 0 ) | 125 | if( iAmt == 0 ) |
126 | { | 126 | { |
127 | return (*this); | 127 | return (*this); |
128 | } | 128 | } |
129 | return (*this); | 129 | return (*this); |
130 | } | 130 | } |
131 | 131 | ||
132 | void Bu::BitString::shiftLeft( long iAmt ) | 132 | void Bu::BitString::shiftLeft( long iAmt ) |
133 | { | 133 | { |
134 | if( iAmt == 0 ) | 134 | if( iAmt == 0 ) |
135 | { | 135 | { |
136 | return; | 136 | return; |
137 | } | 137 | } |
138 | else if( iAmt < 0 ) | 138 | else if( iAmt < 0 ) |
139 | { | 139 | { |
140 | shiftRight( -iAmt ); | 140 | shiftRight( -iAmt ); |
141 | return; | 141 | return; |
142 | } | 142 | } |
143 | 143 | ||
144 | long iByteShift = iAmt/8; | 144 | long iByteShift = iAmt/8; |
145 | long iBitShift = iAmt%8; | 145 | long iBitShift = iAmt%8; |
146 | 146 | ||
147 | long j; | 147 | long j; |
148 | for( j = iBytes-1; j >= 0; j-- ) | 148 | for( j = iBytes-1; j >= 0; j-- ) |
149 | { | 149 | { |
150 | caData[j] = (((j-iByteShift)<0)?(0):((caData[j-iByteShift]<<iBitShift))) | (((j-iByteShift-1)<0)?(0):((caData[j-iByteShift-1]>>(8-iBitShift)))); | 150 | caData[j] = (((j-iByteShift)<0)?(0):((caData[j-iByteShift]<<iBitShift))) | (((j-iByteShift-1)<0)?(0):((caData[j-iByteShift-1]>>(8-iBitShift)))); |
151 | } | 151 | } |
152 | 152 | ||
153 | fixup(); | 153 | fixup(); |
154 | } | 154 | } |
155 | 155 | ||
156 | void Bu::BitString::shiftRight( long iAmt ) | 156 | void Bu::BitString::shiftRight( long iAmt ) |
157 | { | 157 | { |
158 | if( iAmt == 0 ) | 158 | if( iAmt == 0 ) |
159 | { | 159 | { |
160 | return; | 160 | return; |
161 | } | 161 | } |
162 | else if( iAmt < 0 ) | 162 | else if( iAmt < 0 ) |
163 | { | 163 | { |
164 | shiftLeft( -iAmt ); | 164 | shiftLeft( -iAmt ); |
165 | return; | 165 | return; |
166 | } | 166 | } |
167 | 167 | ||
168 | long iByteShift = iAmt/8; | 168 | long iByteShift = iAmt/8; |
169 | long iBitShift = iAmt%8; | 169 | long iBitShift = iAmt%8; |
170 | 170 | ||
171 | long j; | 171 | long j; |
172 | for( j = 0; j < iBytes; j++ ) | 172 | for( j = 0; j < iBytes; j++ ) |
173 | { | 173 | { |
174 | caData[j] = (((j+iByteShift)>iBytes)?(0):((caData[j+iByteShift]>>iBitShift))) | (((j+iByteShift+1)>iBytes)?(0):((caData[j+iByteShift+1]<<(8-iBitShift)))); | 174 | caData[j] = (((j+iByteShift)>iBytes)?(0):((caData[j+iByteShift]>>iBitShift))) | (((j+iByteShift+1)>iBytes)?(0):((caData[j+iByteShift+1]<<(8-iBitShift)))); |
175 | } | 175 | } |
176 | 176 | ||
177 | fixup(); | 177 | fixup(); |
178 | } | 178 | } |
179 | /* | 179 | /* |
180 | long Bu::BitString::bitsToBytes( long iBits ) | 180 | long Bu::BitString::bitsToBytes( long iBits ) |
181 | { | 181 | { |
182 | return (iBits/8)+((iBits%8)?(1):(0)); | 182 | return (iBits/8)+((iBits%8)?(1):(0)); |
183 | } | 183 | } |
184 | */ | 184 | */ |
185 | void Bu::BitString::fixup() | 185 | void Bu::BitString::fixup() |
186 | { | 186 | { |
187 | if( caData != NULL ) | 187 | if( caData != NULL ) |
188 | { | 188 | { |
189 | caData[iBytes-1] &= cTopByteMask; | 189 | caData[iBytes-1] &= cTopByteMask; |
190 | } | 190 | } |
191 | } | 191 | } |
192 | 192 | ||
193 | void Bu::BitString::setBit( long iBit, bool bBitState ) | 193 | void Bu::BitString::setBit( long iBit, bool bBitState ) |
194 | { | 194 | { |
195 | if( iBit < 0 || iBit >= iBits ) | 195 | if( iBit < 0 || iBit >= iBits ) |
196 | throw Bu::ExceptionBase("bit out of range: %d in (0-%d)", iBit, iBits ); | 196 | throw Bu::ExceptionBase("bit out of range: %d in (0-%d)", iBit, iBits ); |
197 | if( bBitState ) | 197 | if( bBitState ) |
198 | { | 198 | { |
199 | caData[iBit/8] |= (1<<(iBit%8)); | 199 | caData[iBit/8] |= (1<<(iBit%8)); |
200 | } | 200 | } |
201 | else | 201 | else |
202 | { | 202 | { |
203 | caData[iBit/8] &= ~(1<<(iBit%8)); | 203 | caData[iBit/8] &= ~(1<<(iBit%8)); |
204 | } | 204 | } |
205 | } | 205 | } |
206 | 206 | ||
207 | void Bu::BitString::flipBit( long iBit ) | 207 | void Bu::BitString::flipBit( long iBit ) |
208 | { | 208 | { |
209 | caData[iBit/8] ^= (1<<(iBit%8)); | 209 | caData[iBit/8] ^= (1<<(iBit%8)); |
210 | } | 210 | } |
211 | 211 | ||
212 | bool Bu::BitString::getBit( long iBit ) | 212 | bool Bu::BitString::getBit( long iBit ) |
213 | { | 213 | { |
214 | if( iBit >= iBits || iBit < 0 ) return false; | 214 | if( iBit >= iBits || iBit < 0 ) return false; |
215 | if( (caData[iBit/8] & (1<<(iBit%8))) == 0 ) | 215 | if( (caData[iBit/8] & (1<<(iBit%8))) == 0 ) |
216 | { | 216 | { |
217 | return false; | 217 | return false; |
218 | } | 218 | } |
219 | return true; | 219 | return true; |
220 | } | 220 | } |
221 | 221 | ||
222 | long Bu::BitString::getBitLength() | 222 | long Bu::BitString::getBitLength() |
223 | { | 223 | { |
224 | return iBits; | 224 | return iBits; |
225 | } | 225 | } |
226 | 226 | ||
227 | long Bu::BitString::getSize() | 227 | long Bu::BitString::getSize() |
228 | { | 228 | { |
229 | return iBits; | 229 | return iBits; |
230 | } | 230 | } |
231 | 231 | ||
232 | class Bu::BitString Bu::BitString::getSubString( long iLower, long iUpper ) | 232 | class Bu::BitString Bu::BitString::getSubString( long iLower, long iUpper ) |
233 | { | 233 | { |
234 | if( iUpper == 0 || iUpper < iLower ) iUpper = iBits; | 234 | if( iUpper == 0 || iUpper < iLower ) iUpper = iBits; |
235 | 235 | ||
236 | Bu::BitString xSub( iUpper-iLower+1 ); | 236 | Bu::BitString xSub( iUpper-iLower+1 ); |
237 | 237 | ||
238 | long shft = (iLower%8); | 238 | long shft = (iLower%8); |
239 | long base = (iLower/8); | 239 | long base = (iLower/8); |
240 | unsigned char lowmask=0; | 240 | unsigned char lowmask=0; |
241 | for( long j = 0; j < 8-shft; j++ ) | 241 | for( long j = 0; j < 8-shft; j++ ) |
242 | { | 242 | { |
243 | lowmask |= (1<<j); | 243 | lowmask |= (1<<j); |
244 | } | 244 | } |
245 | for( long j = 0; j < xSub.iBytes; j++ ) | 245 | for( long j = 0; j < xSub.iBytes; j++ ) |
246 | { | 246 | { |
247 | xSub.caData[j] = ((caData[base+j]>>shft)&(lowmask)) | ((caData[base+j+1]<<(8-shft))&(~lowmask)); | 247 | xSub.caData[j] = ((caData[base+j]>>shft)&(lowmask)) | ((caData[base+j+1]<<(8-shft))&(~lowmask)); |
248 | } | 248 | } |
249 | xSub.fixup(); | 249 | xSub.fixup(); |
250 | 250 | ||
251 | return xSub; | 251 | return xSub; |
252 | } | 252 | } |
253 | 253 | ||
254 | long Bu::BitString::toLong( long iStart, long iSize ) | 254 | long Bu::BitString::toLong( long iStart, long iSize ) |
255 | { | 255 | { |
256 | if( iSize < 1 ) iSize = 1; | 256 | if( iSize < 1 ) iSize = 1; |
257 | if( iSize > 32 ) iSize = 32; | 257 | if( iSize > 32 ) iSize = 32; |
258 | if( iStart < 0 ) return 0; | 258 | if( iStart < 0 ) return 0; |
259 | if( iStart+iSize > getSize() ) return 0; | 259 | if( iStart+iSize > getSize() ) return 0; |
260 | 260 | ||
261 | Bu::BitString tmpo; | 261 | Bu::BitString tmpo; |
262 | tmpo = getSubString( iStart, iStart+iSize-1 ); | 262 | tmpo = getSubString( iStart, iStart+iSize-1 ); |
263 | long x = *((long *)tmpo.caData); | 263 | long x = *((long *)tmpo.caData); |
264 | 264 | ||
265 | return x; | 265 | return x; |
266 | } | 266 | } |
267 | /* | 267 | /* |
268 | std::string Bu::BitString::toString( bool bAddSpacers ) | 268 | std::string Bu::BitString::toString( bool bAddSpacers ) |
269 | { | 269 | { |
270 | long iSz = iBits; | 270 | long iSz = iBits; |
271 | if( bAddSpacers ) | 271 | if( bAddSpacers ) |
272 | { | 272 | { |
273 | iSz += (iBits/8); | 273 | iSz += (iBits/8); |
274 | if( iBits%8 == 0 ) iSz--; | 274 | if( iBits%8 == 0 ) iSz--; |
275 | } | 275 | } |
276 | std::string xStr; | 276 | std::string xStr; |
277 | 277 | ||
278 | int bw=0; | 278 | int bw=0; |
279 | int of=0; | 279 | int of=0; |
280 | for( int j = iBits-1; j >= 0; j-- ) | 280 | for( int j = iBits-1; j >= 0; j-- ) |
281 | { | 281 | { |
282 | if( getBit( j ) ) | 282 | if( getBit( j ) ) |
283 | { | 283 | { |
284 | xStr += '1'; | 284 | xStr += '1'; |
285 | } | 285 | } |
286 | else | 286 | else |
287 | { | 287 | { |
288 | xStr += '0'; | 288 | xStr += '0'; |
289 | } | 289 | } |
290 | 290 | ||
291 | if( bAddSpacers ) | 291 | if( bAddSpacers ) |
292 | { | 292 | { |
293 | bw++; | 293 | bw++; |
294 | if( bw >= 8 && j < iBits-1 ) | 294 | if( bw >= 8 && j < iBits-1 ) |
295 | { | 295 | { |
296 | bw = 0; | 296 | bw = 0; |
297 | of++; | 297 | of++; |
298 | xStr += ' '; | 298 | xStr += ' '; |
299 | } | 299 | } |
300 | } | 300 | } |
301 | } | 301 | } |
302 | 302 | ||
303 | return xStr; | 303 | return xStr; |
304 | } | 304 | } |
305 | */ | 305 | */ |
306 | void Bu::BitString::clear() | 306 | void Bu::BitString::clear() |
307 | { | 307 | { |
308 | if( caData != NULL ) | 308 | if( caData != NULL ) |
309 | { | 309 | { |
310 | memset( caData, 0, iBytes ); | 310 | memset( caData, 0, iBytes ); |
311 | } | 311 | } |
312 | } | 312 | } |
313 | 313 | ||
314 | bool Bu::BitString::setBitLength( long iLength, bool bClear ) | 314 | bool Bu::BitString::setBitLength( long iLength, bool bClear ) |
315 | { | 315 | { |
316 | return setSize( iLength, bClear ); | 316 | return setSize( iLength, bClear ); |
317 | } | 317 | } |
318 | 318 | ||
319 | bool Bu::BitString::setSize( long iLength, bool bClear ) | 319 | bool Bu::BitString::setSize( long iLength, bool bClear ) |
320 | { | 320 | { |
321 | // First, if there's nothing, then allocate an empty one. | 321 | // First, if there's nothing, then allocate an empty one. |
322 | if( caData == NULL ) | 322 | if( caData == NULL ) |
323 | { | 323 | { |
324 | iBits = iLength; | 324 | iBits = iLength; |
325 | iBytes = bitsToBytes( iLength ); | 325 | iBytes = bitsToBytes( iLength ); |
326 | caData = new unsigned char[iBytes]; | 326 | caData = new unsigned char[iBytes]; |
327 | memset( caData, 0, iBytes ); | 327 | memset( caData, 0, iBytes ); |
328 | return true; | 328 | return true; |
329 | } | 329 | } |
330 | 330 | ||
331 | // If the new length is the same as the old, don't do anything, but do | 331 | // If the new length is the same as the old, don't do anything, but do |
332 | // check to see if we should still clear the data. | 332 | // check to see if we should still clear the data. |
333 | if( iBits != iLength ) | 333 | if( iBits != iLength ) |
334 | { | 334 | { |
335 | // Ok, we are changing the number if bits, but are we changing the | 335 | // Ok, we are changing the number if bits, but are we changing the |
336 | // number of bytes? | 336 | // number of bytes? |
337 | long iNewBytes = bitsToBytes( iLength ); | 337 | long iNewBytes = bitsToBytes( iLength ); |
338 | if( iBytes == iNewBytes ) | 338 | if( iBytes == iNewBytes ) |
339 | { | 339 | { |
340 | // No? That makes life easier | 340 | // No? That makes life easier |
341 | iBits = iLength; | 341 | iBits = iLength; |
342 | setMask(); | 342 | setMask(); |
343 | if( bClear ) | 343 | if( bClear ) |
344 | { | 344 | { |
345 | clear(); | 345 | clear(); |
346 | } | 346 | } |
347 | } | 347 | } |
348 | else | 348 | else |
349 | { | 349 | { |
350 | // Ok, reallocate and copy... | 350 | // Ok, reallocate and copy... |
351 | iBits = iLength; | 351 | iBits = iLength; |
352 | // long iNewBytes = bitsToBytes( iLength ); | 352 | // long iNewBytes = bitsToBytes( iLength ); |
353 | if( bClear ) | 353 | if( bClear ) |
354 | { | 354 | { |
355 | delete[] caData; | 355 | delete[] caData; |
356 | caData = new unsigned char[iNewBytes]; | 356 | caData = new unsigned char[iNewBytes]; |
357 | memset( caData, 0, iNewBytes ); | 357 | memset( caData, 0, iNewBytes ); |
358 | } | 358 | } |
359 | else | 359 | else |
360 | { | 360 | { |
361 | unsigned char *tmp = caData; | 361 | unsigned char *tmp = caData; |
362 | caData = new unsigned char[iNewBytes]; | 362 | caData = new unsigned char[iNewBytes]; |
363 | if( iNewBytes < iBytes ) | 363 | if( iNewBytes < iBytes ) |
364 | { | 364 | { |
365 | memcpy( caData, tmp, iNewBytes ); | 365 | memcpy( caData, tmp, iNewBytes ); |
366 | } | 366 | } |
367 | else | 367 | else |
368 | { | 368 | { |
369 | memcpy( caData, tmp, iBytes ); | 369 | memcpy( caData, tmp, iBytes ); |
370 | } | 370 | } |
371 | delete[] tmp; | 371 | delete[] tmp; |
372 | } | 372 | } |
373 | iBytes = iNewBytes; | 373 | iBytes = iNewBytes; |
374 | 374 | ||
375 | setMask(); | 375 | setMask(); |
376 | } | 376 | } |
377 | 377 | ||
378 | } | 378 | } |
379 | else if( bClear ) | 379 | else if( bClear ) |
380 | { | 380 | { |
381 | clear(); | 381 | clear(); |
382 | } | 382 | } |
383 | 383 | ||
384 | return true; | 384 | return true; |
385 | } | 385 | } |
386 | 386 | ||
387 | void Bu::BitString::setMask() | 387 | void Bu::BitString::setMask() |
388 | { | 388 | { |
389 | // This can either mean that there are a multiple of eight bits or | 389 | // This can either mean that there are a multiple of eight bits or |
390 | // zero, if there are zero you're an idiot (zero can't happen, because | 390 | // zero, if there are zero you're an idiot (zero can't happen, because |
391 | // we would allocate an extra byte and never use it) | 391 | // we would allocate an extra byte and never use it) |
392 | if( (iBits%8 == 0) ) | 392 | if( (iBits%8 == 0) ) |
393 | { | 393 | { |
394 | cTopByteMask = 0xFF; | 394 | cTopByteMask = 0xFF; |
395 | } | 395 | } |
396 | else | 396 | else |
397 | { | 397 | { |
398 | cTopByteMask = 0; | 398 | cTopByteMask = 0; |
399 | for( long j = 0; j < (iBits%8); j++ ) | 399 | for( long j = 0; j < (iBits%8); j++ ) |
400 | { | 400 | { |
401 | cTopByteMask |= (1<<j); | 401 | cTopByteMask |= (1<<j); |
402 | } | 402 | } |
403 | } | 403 | } |
404 | } | 404 | } |
405 | 405 | ||
406 | void Bu::BitString::randomize() | 406 | void Bu::BitString::randomize() |
407 | { | 407 | { |
408 | if( caData != NULL ) | 408 | if( caData != NULL ) |
409 | { | 409 | { |
410 | for( int j = 0; j < iBytes; j++ ) | 410 | for( int j = 0; j < iBytes; j++ ) |
411 | { | 411 | { |
412 | caData[j] = (unsigned char)(Bu::Random::rand() & 0xFF); | 412 | caData[j] = (unsigned char)(Bu::Random::rand() & 0xFF); |
413 | } | 413 | } |
414 | fixup(); | 414 | fixup(); |
415 | } | 415 | } |
416 | } | 416 | } |
417 | 417 | ||
418 | void Bu::BitString::invert() | 418 | void Bu::BitString::invert() |
419 | { | 419 | { |
420 | if( caData != NULL ) | 420 | if( caData != NULL ) |
421 | { | 421 | { |
422 | for( long j = 0; j < iBytes; j++ ) | 422 | for( long j = 0; j < iBytes; j++ ) |
423 | { | 423 | { |
424 | caData[j] = ~caData[j]; | 424 | caData[j] = ~caData[j]; |
425 | } | 425 | } |
426 | fixup(); | 426 | fixup(); |
427 | } | 427 | } |
428 | } | 428 | } |
429 | 429 | ||
430 | long Bu::BitString::getHighestOrderBitPos() | 430 | long Bu::BitString::getHighestOrderBitPos() |
431 | { | 431 | { |
432 | for( long j = iBits-1; j >= 0; j-- ) | 432 | for( long j = iBits-1; j >= 0; j-- ) |
433 | { | 433 | { |
434 | if( getBit( j ) ) | 434 | if( getBit( j ) ) |
435 | { | 435 | { |
436 | return j; | 436 | return j; |
437 | } | 437 | } |
438 | } | 438 | } |
439 | 439 | ||
440 | return -1; | 440 | return -1; |
441 | } | 441 | } |
442 | 442 | ||
443 | Bu::String Bu::BitString::toString() | 443 | Bu::String Bu::BitString::toString() |
444 | { | 444 | { |
445 | Bu::String sRet; | 445 | Bu::String sRet; |
446 | for( int j = iBits-1; j >= 0; j-- ) | 446 | for( int j = iBits-1; j >= 0; j-- ) |
447 | sRet.append( getBit( j )?'1':'0' ); | 447 | sRet.append( getBit( j )?'1':'0' ); |
448 | return sRet; | 448 | return sRet; |
449 | } | 449 | } |
450 | 450 | ||
451 | /* | 451 | /* |
452 | bool Bu::BitString::writeToFile( FILE *fh ) | 452 | bool Bu::BitString::writeToFile( FILE *fh ) |
453 | { | 453 | { |
454 | fwrite( &iBits, sizeof(long), 1, fh ); | 454 | fwrite( &iBits, sizeof(long), 1, fh ); |
455 | fwrite( caData, sizeof(char), iBytes, fh ); | 455 | fwrite( caData, sizeof(char), iBytes, fh ); |
456 | 456 | ||
457 | return true; | 457 | return true; |
458 | } | 458 | } |
459 | 459 | ||
460 | bool Bu::BitString::readFromFile( FILE *fh ) | 460 | bool Bu::BitString::readFromFile( FILE *fh ) |
461 | { | 461 | { |
462 | fread( &iBits, sizeof(long), 1, fh ); | 462 | fread( &iBits, sizeof(long), 1, fh ); |
463 | 463 | ||
464 | iBytes = bitsToBytes( iBits ); | 464 | iBytes = bitsToBytes( iBits ); |
465 | if( caData ) delete[] caData; | 465 | if( caData ) delete[] caData; |
466 | caData = new unsigned char[iBytes]; | 466 | caData = new unsigned char[iBytes]; |
467 | 467 | ||
468 | fread( caData, sizeof(char), iBytes, fh ); | 468 | fread( caData, sizeof(char), iBytes, fh ); |
469 | 469 | ||
470 | setMask(); | 470 | setMask(); |
471 | 471 | ||
472 | fixup(); | 472 | fixup(); |
473 | 473 | ||
474 | return true; | 474 | return true; |
475 | }*/ | 475 | }*/ |