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