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