Logo ROOT  
Reference Guide
TBits.cxx
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Philippe Canal 05/02/2001
3// Feb 5 2001: Creation
4// Feb 6 2001: Changed all int to unsigned int.
5
6/** \class TBits
7\ingroup Containers
8Container of bits.
9
10This class provides a simple container of bits.
11Each bit can be set and tested via the functions SetBitNumber and
12TestBitNumber.
13 .
14The default value of all bits is kFALSE.
15The size of the container is automatically extended when a bit
16number is either set or tested. To reduce the memory size of the
17container use the Compact function, this will discard the memory
18occupied by the upper bits that are 0.
19*/
20
21#include "TBits.h"
22
23#include "Riostream.h"
24#include "TObject.h"
25
26#include <string.h>
27
29
30////////////////////////////////////////////////////////////////////////////////
31/// TBits constructor. All bits set to 0
32
33TBits::TBits(UInt_t nbits) : fNbits(nbits)
34{
35 if (nbits <= 0) nbits = 8;
36 fNbytes = ((nbits-1)/8) + 1;
38 // this is redundant only with libNew
39 memset(fAllBits,0,fNbytes);
40}
41
42////////////////////////////////////////////////////////////////////////////////
43/// TBits copy constructor
44
45TBits::TBits(const TBits &original) : TObject(original), fNbits(original.fNbits),
46 fNbytes(original.fNbytes)
47{
49 memcpy(fAllBits,original.fAllBits,fNbytes);
50
51}
52
53////////////////////////////////////////////////////////////////////////////////
54/// TBits assignment operator
55
57{
58 if (this != &rhs) {
60 fNbits = rhs.fNbits;
61 fNbytes = rhs.fNbytes;
62 delete [] fAllBits;
63 if (fNbytes != 0) {
65 memcpy(fAllBits,rhs.fAllBits,fNbytes);
66 } else {
67 fAllBits = 0;
68 }
69 }
70 return *this;
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// TBits destructor
75
77{
78 delete [] fAllBits;
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Clear the value.
83
84void TBits::Clear(Option_t * /*option*/)
85{
86 delete [] fAllBits;
87 fAllBits = 0;
88 fNbits = 0;
89 fNbytes = 0;
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// Reduce the storage used by the object to a minimun
94
96{
97 if (!fNbits || !fAllBits) return;
98 UInt_t needed;
99 for(needed=fNbytes-1;
100 needed > 0 && fAllBits[needed]==0; ) { needed--; };
101 needed++;
102
103 if (needed!=fNbytes) {
104 UChar_t *old_location = fAllBits;
105 fAllBits = new UChar_t[needed];
106
107 memcpy(fAllBits,old_location,needed);
108 delete [] old_location;
109
110 fNbytes = needed;
111 fNbits = 8*fNbytes;
112 }
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Return number of bits set to 1 starting at bit startBit
117
119{
120 static const Int_t nbits[256] = {
121 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
122 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
123 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
124 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
125 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
126 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
127 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
128 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
129 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
130 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
131 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
132 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
133 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
134 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
135 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
136 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
137
138 UInt_t i,count = 0;
139 if (startBit == 0) {
140 for(i=0; i<fNbytes; i++) {
141 count += nbits[fAllBits[i]];
142 }
143 return count;
144 }
145 if (startBit >= fNbits) return count;
146 UInt_t startByte = startBit/8;
147 UInt_t ibit = startBit%8;
148 if (ibit) {
149 for (i=ibit;i<8;i++) {
150 if (fAllBits[startByte] & (1<<ibit)) count++;
151 }
152 startByte++;
153 }
154 for(i=startByte; i<fNbytes; i++) {
155 count += nbits[fAllBits[i]];
156 }
157 return count;
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Execute `(*this) &= rhs;`
162/// Extra bits in rhs are ignored
163/// Missing bits in rhs are assumed to be zero.
164
165void TBits::DoAndEqual(const TBits& rhs)
166{
167 UInt_t min = (fNbytes<rhs.fNbytes) ? fNbytes : rhs.fNbytes;
168 for(UInt_t i=0; i<min; ++i) {
169 fAllBits[i] &= rhs.fAllBits[i];
170 }
171 if (fNbytes>min) {
172 memset(&(fAllBits[min]),0,fNbytes-min);
173 }
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Execute `(*this) &= rhs;`
178/// Extra bits in rhs are ignored
179/// Missing bits in rhs are assumed to be zero.
180
181void TBits::DoOrEqual(const TBits& rhs)
182{
183 UInt_t min = (fNbytes<rhs.fNbytes) ? fNbytes : rhs.fNbytes;
184 for(UInt_t i=0; i<min; ++i) {
185 fAllBits[i] |= rhs.fAllBits[i];
186 }
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// Execute `(*this) ^= rhs;`
191/// Extra bits in rhs are ignored
192/// Missing bits in rhs are assumed to be zero.
193
194void TBits::DoXorEqual(const TBits& rhs)
195{
196 UInt_t min = (fNbytes<rhs.fNbytes) ? fNbytes : rhs.fNbytes;
197 for(UInt_t i=0; i<min; ++i) {
198 fAllBits[i] ^= rhs.fAllBits[i];
199 }
200}
201
202////////////////////////////////////////////////////////////////////////////////
203/// Execute `~(*this)`
204
206{
207 for(UInt_t i=0; i<fNbytes; ++i) {
208 fAllBits[i] = ~fAllBits[i];
209 }
210 // NOTE: out-of-bounds bit were also flipped!
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Execute the left shift operation.
215/// Note: This does extent the number of bits (i.e. no data loss)
216
218{
219 if (shift==0) return;
220 const UInt_t wordshift = shift / 8;
221 const UInt_t offset = shift % 8;
222 Resize(fNbits + shift);
223 if (offset==0) {
224 for(UInt_t n = fNbytes - 1; n >= wordshift; --n) {
225 fAllBits[n] = fAllBits[ n - wordshift ];
226 }
227 } else {
228 const UInt_t sub_offset = 8 - offset;
229 for(UInt_t n = fNbytes - 1; n > wordshift; --n) {
230 fAllBits[n] = (fAllBits[n - wordshift] << offset) |
231 (fAllBits[n - wordshift - 1] >> sub_offset);
232 }
233 fAllBits[wordshift] = fAllBits[0] << offset;
234 }
235 memset(fAllBits,0,wordshift);
236 fNbits += shift;
237}
238
239////////////////////////////////////////////////////////////////////////////////
240/// Execute the left shift operation.
241
243{
244 if (shift==0) return;
245 const UInt_t wordshift = shift / 8;
246 const UInt_t offset = shift % 8;
247 if (fNbytes < (wordshift + 1)) {
248 memset(fAllBits, 0, fNbytes);
249 fNbits = 0;
250 } else {
251 const UInt_t limit = fNbytes - wordshift - 1;
252
253 if (offset == 0)
254 for (UInt_t n = 0; n <= limit; ++n)
255 fAllBits[n] = fAllBits[n + wordshift];
256 else {
257 const UInt_t sub_offset = 8 - offset;
258 for (UInt_t n = 0; n < limit; ++n)
259 fAllBits[n] = (fAllBits[n + wordshift] >> offset) |
260 (fAllBits[n + wordshift + 1] << sub_offset);
261 fAllBits[limit] = fAllBits[fNbytes - 1] >> offset;
262 }
263
264 memset(&(fAllBits[limit + 1]), 0, fNbytes - limit - 1);
265 if (fNbits < shift)
266 fNbits = 0;
267 else
268 fNbits -= shift;
269 }
270}
271
272////////////////////////////////////////////////////////////////////////////////
273/// Return position of first null bit (starting from position 0 and up)
274
276{
277 static const Int_t fbits[256] = {
278 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
279 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,
280 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
281 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,
282 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
283 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,
284 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
285 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,7,
286 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
287 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,
288 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
289 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,
290 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
291 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,
292 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
293 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,8};
294
295 UInt_t i;
296 if (startBit == 0) {
297 for(i=0; i<fNbytes; i++) {
298 if (fAllBits[i] != 255) return 8*i + fbits[fAllBits[i]];
299 }
300 return fNbits;
301 }
302 if (startBit >= fNbits) return fNbits;
303 UInt_t startByte = startBit/8;
304 UInt_t ibit = startBit%8;
305 if (ibit) {
306 for (i=ibit;i<8;i++) {
307 if ((fAllBits[startByte] & (1<<i)) == 0) return 8*startByte+i;
308 }
309 startByte++;
310 }
311 for(i=startByte; i<fNbytes; i++) {
312 if (fAllBits[i] != 255) return 8*i + fbits[fAllBits[i]];
313 }
314 return fNbits;
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// Return position of first null bit (starting from position 0 and up)
319
321{
322 static const Int_t fbits[256] = {
323 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
324 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
325 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
326 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
327 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
328 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
329 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
330 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
331 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
332 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
333 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
334 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
335 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
336 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
337 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
338 3,3,3,3,3,3,3,3,2,2,2,2,1,1,0,8};
339
340 UInt_t i;
341 if (startBit>=fNbits) startBit = fNbits-1;
342 UInt_t startByte = startBit/8;
343 UInt_t ibit = startBit%8;
344 if (ibit<7) {
345 for (i=ibit+1;i>0;i--) {
346 if ((fAllBits[startByte] & (1<<(i-1))) == 0) return 8*startByte+i-1;
347 }
348 startByte--;
349 }
350 for(i=startByte+1; i>0; i--) {
351 if (fAllBits[i-1] != 255) return 8*(i-1) + fbits[fAllBits[i-1]];
352 }
353 return fNbits;
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Return position of first non null bit (starting from position 0 and up)
358
360{
361 static const Int_t fbits[256] = {
362 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
363 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
364 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
365 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
366 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
367 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
368 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
369 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
370 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
371 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
372 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
373 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
374 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
375 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
376 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
377 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
378
379 UInt_t i;
380 if (startBit == 0) {
381 for(i=0; i<fNbytes; i++) {
382 if (fAllBits[i] != 0) return 8*i + fbits[fAllBits[i]];
383 }
384 return fNbits;
385 }
386 if (startBit >= fNbits) return fNbits;
387 UInt_t startByte = startBit/8;
388 UInt_t ibit = startBit%8;
389 if (ibit) {
390 for (i=ibit;i<8;i++) {
391 if ((fAllBits[startByte] & (1<<i)) != 0) return 8*startByte+i;
392 }
393 startByte++;
394 }
395 for(i=startByte; i<fNbytes; i++) {
396 if (fAllBits[i] != 0) return 8*i + fbits[fAllBits[i]];
397 }
398 return fNbits;
399}
400
401////////////////////////////////////////////////////////////////////////////////
402/// Return position of first non null bit (starting from position 0 and up)
403
405{
406 static const Int_t fbits[256] = {
407 8,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
408 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
409 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
410 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
411 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
412 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
413 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
414 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
415 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
416 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
417 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
418 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
419 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
420 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
421 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
422 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
423
424 UInt_t i;
425 if (startBit>=fNbits) startBit = fNbits-1;
426 UInt_t startByte = startBit/8;
427 UInt_t ibit = startBit%8;
428 if (ibit<7) {
429 for (i=ibit+1;i>0;i--) {
430 if ((fAllBits[startByte] & (1<<(i-1))) != 0) return 8*startByte+i-1;
431 }
432 startByte--;
433 }
434 for(i=startByte+1; i>0; i--) {
435 if (fAllBits[i-1] != 0) return 8*(i-1) + fbits[fAllBits[i-1]];
436 }
437 return fNbits;
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Print the value to the std::ostream
442
443void TBits::Output(std::ostream &os) const
444{
445 for(UInt_t i=0; i<fNbytes; ++i) {
446 UChar_t val = fAllBits[fNbytes - 1 - i];
447 for (UInt_t j=0; j<8; ++j) {
448 os << (Bool_t)(val&0x80);
449 val <<= 1;
450 }
451 }
452}
453
454////////////////////////////////////////////////////////////////////////////////
455/// Once implemented, it will draw the bit field as an histogram.
456/// use the TVirtualPainter as the usual trick
457
459{
460}
461
462////////////////////////////////////////////////////////////////////////////////
463/// Print the list of active bits
464
466{
467 Int_t count = 0;
468 for(UInt_t i=0; i<fNbytes; ++i) {
469 UChar_t val = fAllBits[i];
470 for (UInt_t j=0; j<8; ++j) {
471 if (val & 1) printf(" bit:%4d = 1\n",count);
472 count++;
473 val = val >> 1;
474 }
475 }
476}
477
478////////////////////////////////////////////////////////////////////////////////
479/// Reset all bits to 0 (false).
480
482{
483 if (fAllBits) memset(fAllBits,0,fNbytes);
484}
485
486////////////////////////////////////////////////////////////////////////////////
487/// Reverse each bytes.
488
490{
491 if (nbytes > fNbytes) {
492 // do it in this order to remain exception-safe.
493 UChar_t *newBits=new UChar_t[nbytes];
494 delete[] fAllBits;
495 fNbytes=nbytes;
496 fAllBits=newBits;
497 }
498}
499
500////////////////////////////////////////////////////////////////////////////////
501/// Set all the bytes.
502
503void TBits::Set(UInt_t nbits, const Char_t *array)
504{
505 UInt_t nbytes=(nbits+7)>>3;
506
507 ReserveBytes(nbytes);
508
509 fNbits=nbits;
510 memcpy(fAllBits, array, nbytes);
511}
512
513////////////////////////////////////////////////////////////////////////////////
514/// Copy all the byes.
515
516void TBits::Get(Char_t *array) const
517{
518 memcpy(array, fAllBits, (fNbits+7)>>3);
519}
520
521 #ifdef R__BYTESWAP /* means we are on little endian */
522
523 /*
524 If we are on a little endian machine, a bitvector represented using
525 any integer type is identical to a bitvector represented using bytes.
526 -- FP.
527 */
528
529void TBits::Set(UInt_t nbits, const Short_t *array)
530{
531 // Set all the bytes.
532
533 Set(nbits, (const Char_t*)array);
534}
535
536void TBits::Set(UInt_t nbits, const Int_t *array)
537{
538 // Set all the bytes.
539
540 Set(nbits, (const Char_t*)array);
541}
542
543void TBits::Set(UInt_t nbits, const Long64_t *array)
544{
545 // Set all the bytes.
546
547 Set(nbits, (const Char_t*)array);
548}
549
550void TBits::Get(Short_t *array) const
551{
552 // Get all the bytes.
553
554 Get((Char_t*)array);
555}
556
557void TBits::Get(Int_t *array) const
558{
559 // Get all the bytes.
560
561 Get((Char_t*)array);
562}
563
564void TBits::Get(Long64_t *array) const
565{
566 // Get all the bytes.
567
568 Get((Char_t*)array);
569}
570
571#else
572
573 /*
574 If we are on a big endian machine, some swapping around is required.
575 */
576
577void TBits::Set(UInt_t nbits, const Short_t *array)
578{
579 // make nbytes even so that the loop below is neat.
580 UInt_t nbytes = ((nbits+15)>>3)&~1;
581
582 ReserveBytes(nbytes);
583
584 fNbits=nbits;
585
586 const UChar_t *cArray = (const UChar_t*)array;
587 for (UInt_t i=0; i<nbytes; i+=2) {
588 fAllBits[i] = cArray[i+1];
589 fAllBits[i+1] = cArray[i];
590 }
591}
592
593void TBits::Set(UInt_t nbits, const Int_t *array)
594{
595 // make nbytes a multiple of 4 so that the loop below is neat.
596 UInt_t nbytes = ((nbits+31)>>3)&~3;
597
598 ReserveBytes(nbytes);
599
600 fNbits=nbits;
601
602 const UChar_t *cArray = (const UChar_t*)array;
603 for (UInt_t i=0; i<nbytes; i+=4) {
604 fAllBits[i] = cArray[i+3];
605 fAllBits[i+1] = cArray[i+2];
606 fAllBits[i+2] = cArray[i+1];
607 fAllBits[i+3] = cArray[i];
608 }
609}
610
611void TBits::Set(UInt_t nbits, const Long64_t *array)
612{
613 // make nbytes a multiple of 8 so that the loop below is neat.
614 UInt_t nbytes = ((nbits+63)>>3)&~7;
615
616 ReserveBytes(nbytes);
617
618 fNbits=nbits;
619
620 const UChar_t *cArray = (const UChar_t*)array;
621 for (UInt_t i=0; i<nbytes; i+=8) {
622 fAllBits[i] = cArray[i+7];
623 fAllBits[i+1] = cArray[i+6];
624 fAllBits[i+2] = cArray[i+5];
625 fAllBits[i+3] = cArray[i+4];
626 fAllBits[i+4] = cArray[i+3];
627 fAllBits[i+5] = cArray[i+2];
628 fAllBits[i+6] = cArray[i+1];
629 fAllBits[i+7] = cArray[i];
630 }
631}
632
633void TBits::Get(Short_t *array) const
634{
635 // Get all the bytes.
636
637 UInt_t nBytes = (fNbits+7)>>3;
638 UInt_t nSafeBytes = nBytes&~1;
639
640 UChar_t *cArray=(UChar_t*)array;
641 for (UInt_t i=0; i<nSafeBytes; i+=2) {
642 cArray[i] = fAllBits[i+1];
643 cArray[i+1] = fAllBits[i];
644 }
645
646 if (nBytes>nSafeBytes) {
647 cArray[nSafeBytes+1] = fAllBits[nSafeBytes];
648 }
649}
650
651void TBits::Get(Int_t *array) const
652{
653 // Get all the bytes.
654
655 UInt_t nBytes = (fNbits+7)>>3;
656 UInt_t nSafeBytes = nBytes&~3;
657
658 UChar_t *cArray=(UChar_t*)array;
659 UInt_t i;
660 for (i=0; i<nSafeBytes; i+=4) {
661 cArray[i] = fAllBits[i+3];
662 cArray[i+1] = fAllBits[i+2];
663 cArray[i+2] = fAllBits[i+1];
664 cArray[i+3] = fAllBits[i];
665 }
666
667 for (i=0; i<nBytes-nSafeBytes; ++i) {
668 cArray[nSafeBytes + (3 - i)] = fAllBits[nSafeBytes + i];
669 }
670}
671
672void TBits::Get(Long64_t *array) const
673{
674 // Get all the bytes.
675
676 UInt_t nBytes = (fNbits+7)>>3;
677 UInt_t nSafeBytes = nBytes&~7;
678
679 UChar_t *cArray=(UChar_t*)array;
680 UInt_t i;
681 for (i=0; i<nSafeBytes; i+=8) {
682 cArray[i] = fAllBits[i+7];
683 cArray[i+1] = fAllBits[i+6];
684 cArray[i+2] = fAllBits[i+5];
685 cArray[i+3] = fAllBits[i+4];
686 cArray[i+4] = fAllBits[i+3];
687 cArray[i+5] = fAllBits[i+2];
688 cArray[i+6] = fAllBits[i+1];
689 cArray[i+7] = fAllBits[i];
690 }
691
692 for (i=0; i<nBytes-nSafeBytes; ++i) {
693 cArray[nSafeBytes + (7 - i)] = fAllBits[nSafeBytes + i];
694 }
695}
696
697#endif
698
699Bool_t TBits::operator==(const TBits &other) const
700{
701 // Compare object.
702
703 if (fNbits == other.fNbits) {
704 return !memcmp(fAllBits, other.fAllBits, (fNbits+7)>>3);
705 } else if (fNbits < other.fNbits) {
706 return !memcmp(fAllBits, other.fAllBits, (fNbits+7)>>3) && other.FirstSetBit(fNbits) == other.fNbits;
707 } else {
708 return !memcmp(fAllBits, other.fAllBits, (other.fNbits+7)>>3) && FirstSetBit(other.fNbits) == fNbits;
709 }
710}
711
int Int_t
Definition: RtypesCore.h:41
unsigned char UChar_t
Definition: RtypesCore.h:34
char Char_t
Definition: RtypesCore.h:29
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
short Short_t
Definition: RtypesCore.h:35
long long Long64_t
Definition: RtypesCore.h:69
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
Container of bits.
Definition: TBits.h:27
UInt_t fNbytes
Definition: TBits.h:32
void DoRightShift(UInt_t shift)
Execute the left shift operation.
Definition: TBits.cxx:242
void DoAndEqual(const TBits &rhs)
Execute (*this) &= rhs; Extra bits in rhs are ignored Missing bits in rhs are assumed to be zero.
Definition: TBits.cxx:165
Bool_t operator==(const TBits &other) const
Definition: TBits.cxx:699
void Resize(UInt_t newlen)
Definition: TBits.h:191
void Clear(Option_t *option="")
Clear the value.
Definition: TBits.cxx:84
void DoXorEqual(const TBits &rhs)
Execute (*this) ^= rhs; Extra bits in rhs are ignored Missing bits in rhs are assumed to be zero.
Definition: TBits.cxx:194
void Print(Option_t *option="") const
Print the list of active bits.
Definition: TBits.cxx:465
UInt_t LastNullBit(UInt_t startBit=999999999) const
Return position of first null bit (starting from position 0 and up)
Definition: TBits.cxx:320
UInt_t fNbits
Definition: TBits.h:31
void ResetAllBits(Bool_t value=kFALSE)
Reset all bits to 0 (false).
Definition: TBits.cxx:481
void ReserveBytes(UInt_t nbytes)
Reverse each bytes.
Definition: TBits.cxx:489
void DoOrEqual(const TBits &rhs)
Execute (*this) &= rhs; Extra bits in rhs are ignored Missing bits in rhs are assumed to be zero.
Definition: TBits.cxx:181
void Paint(Option_t *option="")
Once implemented, it will draw the bit field as an histogram.
Definition: TBits.cxx:458
void Output(std::ostream &) const
Print the value to the std::ostream.
Definition: TBits.cxx:443
void Get(Char_t *array) const
Copy all the byes.
Definition: TBits.cxx:516
UInt_t FirstNullBit(UInt_t startBit=0) const
Return position of first null bit (starting from position 0 and up)
Definition: TBits.cxx:275
UInt_t FirstSetBit(UInt_t startBit=0) const
Return position of first non null bit (starting from position 0 and up)
Definition: TBits.cxx:359
UInt_t CountBits(UInt_t startBit=0) const
Return number of bits set to 1 starting at bit startBit.
Definition: TBits.cxx:118
void Compact()
Reduce the storage used by the object to a minimun.
Definition: TBits.cxx:95
void DoLeftShift(UInt_t shift)
Execute the left shift operation.
Definition: TBits.cxx:217
virtual ~TBits()
TBits destructor.
Definition: TBits.cxx:76
UChar_t * fAllBits
Definition: TBits.h:33
TBits & operator=(const TBits &)
TBits assignment operator.
Definition: TBits.cxx:56
TBits(UInt_t nbits=8)
TBits constructor. All bits set to 0.
Definition: TBits.cxx:33
void DoFlip()
Execute ~(*this)
Definition: TBits.cxx:205
void Set(UInt_t nbits, const Char_t *array)
Set all the bytes.
Definition: TBits.cxx:503
UInt_t LastSetBit(UInt_t startBit=999999999) const
Return position of first non null bit (starting from position 0 and up)
Definition: TBits.cxx:404
Mother of all ROOT objects.
Definition: TObject.h:37
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:268
const Int_t n
Definition: legend1.C:16