Logo ROOT   6.10/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 
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 
33 TBits::TBits(UInt_t nbits) : fNbits(nbits)
34 {
35  if (nbits <= 0) nbits = 8;
36  fNbytes = ((nbits-1)/8) + 1;
37  fAllBits = new UChar_t[fNbytes];
38  // this is redundant only with libNew
39  memset(fAllBits,0,fNbytes);
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// TBits copy constructor
44 
45 TBits::TBits(const TBits &original) : TObject(original), fNbits(original.fNbits),
46  fNbytes(original.fNbytes)
47 {
48  fAllBits = new UChar_t[fNbytes];
49  memcpy(fAllBits,original.fAllBits,fNbytes);
50 
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// TBits assignment operator
55 
57 {
58  if (this != &rhs) {
59  TObject::operator=(rhs);
60  fNbits = rhs.fNbits;
61  fNbytes = rhs.fNbytes;
62  delete [] fAllBits;
63  if (fNbytes != 0) {
64  fAllBits = new UChar_t[fNbytes];
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 
84 void 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 
165 void 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 
181 void 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 
194 void 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 
217 {
218  if (shift==0) return;
219  const UInt_t wordshift = shift / 8;
220  const UInt_t offset = shift % 8;
221  if (offset==0) {
222  for(UInt_t n = fNbytes - 1; n >= wordshift; --n) {
223  fAllBits[n] = fAllBits[ n - wordshift ];
224  }
225  } else {
226  const UInt_t sub_offset = 8 - offset;
227  for(UInt_t n = fNbytes - 1; n > wordshift; --n) {
228  fAllBits[n] = (fAllBits[n - wordshift] << offset) |
229  (fAllBits[n - wordshift - 1] >> sub_offset);
230  }
231  fAllBits[wordshift] = fAllBits[0] << offset;
232  }
233  memset(fAllBits,0,wordshift);
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// Execute the left shift operation.
238 
240 {
241  if (shift==0) return;
242  const UInt_t wordshift = shift / 8;
243  const UInt_t offset = shift % 8;
244  const UInt_t limit = fNbytes - wordshift - 1;
245 
246  if (offset == 0)
247  for (UInt_t n = 0; n <= limit; ++n)
248  fAllBits[n] = fAllBits[n + wordshift];
249  else
250  {
251  const UInt_t sub_offset = 8 - offset;
252  for (UInt_t n = 0; n < limit; ++n)
253  fAllBits[n] = (fAllBits[n + wordshift] >> offset) |
254  (fAllBits[n + wordshift + 1] << sub_offset);
255  fAllBits[limit] = fAllBits[fNbytes-1] >> offset;
256  }
257 
258  memset(&(fAllBits[limit + 1]),0, fNbytes - limit - 1);
259 }
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// Return position of first null bit (starting from position 0 and up)
263 
265 {
266  static const Int_t fbits[256] = {
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,6,
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,7,
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,6,
279  0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
280  0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,
281  0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,
282  0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,8};
283 
284  UInt_t i;
285  if (startBit == 0) {
286  for(i=0; i<fNbytes; i++) {
287  if (fAllBits[i] != 255) return 8*i + fbits[fAllBits[i]];
288  }
289  return fNbits;
290  }
291  if (startBit >= fNbits) return fNbits;
292  UInt_t startByte = startBit/8;
293  UInt_t ibit = startBit%8;
294  if (ibit) {
295  for (i=ibit;i<8;i++) {
296  if ((fAllBits[startByte] & (1<<i)) == 0) return 8*startByte+i;
297  }
298  startByte++;
299  }
300  for(i=startByte; i<fNbytes; i++) {
301  if (fAllBits[i] != 255) return 8*i + fbits[fAllBits[i]];
302  }
303  return fNbits;
304 }
305 
306 ////////////////////////////////////////////////////////////////////////////////
307 /// Return position of first null bit (starting from position 0 and up)
308 
310 {
311  static const Int_t fbits[256] = {
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  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
318  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
319  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
320  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
321  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
322  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
323  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
324  5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
325  5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
326  4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
327  3,3,3,3,3,3,3,3,2,2,2,2,1,1,0,8};
328 
329  UInt_t i;
330  if (startBit>=fNbits) startBit = fNbits-1;
331  UInt_t startByte = startBit/8;
332  UInt_t ibit = startBit%8;
333  if (ibit<7) {
334  for (i=ibit+1;i>0;i--) {
335  if ((fAllBits[startByte] & (1<<(i-1))) == 0) return 8*startByte+i-1;
336  }
337  startByte--;
338  }
339  for(i=startByte+1; i>0; i--) {
340  if (fAllBits[i-1] != 255) return 8*(i-1) + fbits[fAllBits[i-1]];
341  }
342  return fNbits;
343 }
344 
345 ////////////////////////////////////////////////////////////////////////////////
346 /// Return position of first non null bit (starting from position 0 and up)
347 
349 {
350  static const Int_t fbits[256] = {
351  8,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  6,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  7,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  6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
364  4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
365  5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
366  4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
367 
368  UInt_t i;
369  if (startBit == 0) {
370  for(i=0; i<fNbytes; i++) {
371  if (fAllBits[i] != 0) return 8*i + fbits[fAllBits[i]];
372  }
373  return fNbits;
374  }
375  if (startBit >= fNbits) return fNbits;
376  UInt_t startByte = startBit/8;
377  UInt_t ibit = startBit%8;
378  if (ibit) {
379  for (i=ibit;i<8;i++) {
380  if ((fAllBits[startByte] & (1<<i)) != 0) return 8*startByte+i;
381  }
382  startByte++;
383  }
384  for(i=startByte; i<fNbytes; i++) {
385  if (fAllBits[i] != 0) return 8*i + fbits[fAllBits[i]];
386  }
387  return fNbits;
388 }
389 
390 ////////////////////////////////////////////////////////////////////////////////
391 /// Return position of first non null bit (starting from position 0 and up)
392 
394 {
395  static const Int_t fbits[256] = {
396  8,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
397  4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
398  5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
399  5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
400  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
401  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
402  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
403  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
410  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
411  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
412 
413  UInt_t i;
414  if (startBit>=fNbits) startBit = fNbits-1;
415  UInt_t startByte = startBit/8;
416  UInt_t ibit = startBit%8;
417  if (ibit<7) {
418  for (i=ibit+1;i>0;i--) {
419  if ((fAllBits[startByte] & (1<<(i-1))) != 0) return 8*startByte+i-1;
420  }
421  startByte--;
422  }
423  for(i=startByte+1; i>0; i--) {
424  if (fAllBits[i-1] != 0) return 8*(i-1) + fbits[fAllBits[i-1]];
425  }
426  return fNbits;
427 }
428 
429 ////////////////////////////////////////////////////////////////////////////////
430 /// Print the value to the std::ostream
431 
432 void TBits::Output(std::ostream &os) const
433 {
434  for(UInt_t i=0; i<fNbytes; ++i) {
435  UChar_t val = fAllBits[fNbytes - 1 - i];
436  for (UInt_t j=0; j<8; ++j) {
437  os << (Bool_t)(val&0x80);
438  val <<= 1;
439  }
440  }
441 }
442 
443 ////////////////////////////////////////////////////////////////////////////////
444 /// Once implemented, it will draw the bit field as an histogram.
445 /// use the TVirtualPainter as the usual trick
446 
448 {
449 }
450 
451 ////////////////////////////////////////////////////////////////////////////////
452 /// Print the list of active bits
453 
454 void TBits::Print(Option_t *) const
455 {
456  Int_t count = 0;
457  for(UInt_t i=0; i<fNbytes; ++i) {
458  UChar_t val = fAllBits[i];
459  for (UInt_t j=0; j<8; ++j) {
460  if (val & 1) printf(" bit:%4d = 1\n",count);
461  count++;
462  val = val >> 1;
463  }
464  }
465 }
466 
467 ////////////////////////////////////////////////////////////////////////////////
468 /// Reset all bits to 0 (false).
469 
471 {
472  if (fAllBits) memset(fAllBits,0,fNbytes);
473 }
474 
475 ////////////////////////////////////////////////////////////////////////////////
476 /// Reverse each bytes.
477 
479 {
480  if (nbytes > fNbytes) {
481  // do it in this order to remain exception-safe.
482  UChar_t *newBits=new UChar_t[nbytes];
483  delete[] fAllBits;
484  fNbytes=nbytes;
485  fAllBits=newBits;
486  }
487 }
488 
489 ////////////////////////////////////////////////////////////////////////////////
490 /// Set all the bytes.
491 
492 void TBits::Set(UInt_t nbits, const Char_t *array)
493 {
494  UInt_t nbytes=(nbits+7)>>3;
495 
496  ReserveBytes(nbytes);
497 
498  fNbits=nbits;
499  memcpy(fAllBits, array, nbytes);
500 }
501 
502 ////////////////////////////////////////////////////////////////////////////////
503 /// Copy all the byes.
504 
505 void TBits::Get(Char_t *array) const
506 {
507  memcpy(array, fAllBits, (fNbits+7)>>3);
508 }
509 
510  #ifdef R__BYTESWAP /* means we are on little endian */
511 
512  /*
513  If we are on a little endian machine, a bitvector represented using
514  any integer type is identical to a bitvector represented using bytes.
515  -- FP.
516  */
517 
518 void TBits::Set(UInt_t nbits, const Short_t *array)
519 {
520  // Set all the bytes.
521 
522  Set(nbits, (const Char_t*)array);
523 }
524 
525 void TBits::Set(UInt_t nbits, const Int_t *array)
526 {
527  // Set all the bytes.
528 
529  Set(nbits, (const Char_t*)array);
530 }
531 
532 void TBits::Set(UInt_t nbits, const Long64_t *array)
533 {
534  // Set all the bytes.
535 
536  Set(nbits, (const Char_t*)array);
537 }
538 
539 void TBits::Get(Short_t *array) const
540 {
541  // Get all the bytes.
542 
543  Get((Char_t*)array);
544 }
545 
546 void TBits::Get(Int_t *array) const
547 {
548  // Get all the bytes.
549 
550  Get((Char_t*)array);
551 }
552 
553 void TBits::Get(Long64_t *array) const
554 {
555  // Get all the bytes.
556 
557  Get((Char_t*)array);
558 }
559 
560 #else
561 
562  /*
563  If we are on a big endian machine, some swapping around is required.
564  */
565 
566 void TBits::Set(UInt_t nbits, const Short_t *array)
567 {
568  // make nbytes even so that the loop below is neat.
569  UInt_t nbytes = ((nbits+15)>>3)&~1;
570 
571  ReserveBytes(nbytes);
572 
573  fNbits=nbits;
574 
575  const UChar_t *cArray = (const UChar_t*)array;
576  for (UInt_t i=0; i<nbytes; i+=2) {
577  fAllBits[i] = cArray[i+1];
578  fAllBits[i+1] = cArray[i];
579  }
580 }
581 
582 void TBits::Set(UInt_t nbits, const Int_t *array)
583 {
584  // make nbytes a multiple of 4 so that the loop below is neat.
585  UInt_t nbytes = ((nbits+31)>>3)&~3;
586 
587  ReserveBytes(nbytes);
588 
589  fNbits=nbits;
590 
591  const UChar_t *cArray = (const UChar_t*)array;
592  for (UInt_t i=0; i<nbytes; i+=4) {
593  fAllBits[i] = cArray[i+3];
594  fAllBits[i+1] = cArray[i+2];
595  fAllBits[i+2] = cArray[i+1];
596  fAllBits[i+3] = cArray[i];
597  }
598 }
599 
600 void TBits::Set(UInt_t nbits, const Long64_t *array)
601 {
602  // make nbytes a multiple of 8 so that the loop below is neat.
603  UInt_t nbytes = ((nbits+63)>>3)&~7;
604 
605  ReserveBytes(nbytes);
606 
607  fNbits=nbits;
608 
609  const UChar_t *cArray = (const UChar_t*)array;
610  for (UInt_t i=0; i<nbytes; i+=8) {
611  fAllBits[i] = cArray[i+7];
612  fAllBits[i+1] = cArray[i+6];
613  fAllBits[i+2] = cArray[i+5];
614  fAllBits[i+3] = cArray[i+4];
615  fAllBits[i+4] = cArray[i+3];
616  fAllBits[i+5] = cArray[i+2];
617  fAllBits[i+6] = cArray[i+1];
618  fAllBits[i+7] = cArray[i];
619  }
620 }
621 
622 void TBits::Get(Short_t *array) const
623 {
624  // Get all the bytes.
625 
626  UInt_t nBytes = (fNbits+7)>>3;
627  UInt_t nSafeBytes = nBytes&~1;
628 
629  UChar_t *cArray=(UChar_t*)array;
630  for (UInt_t i=0; i<nSafeBytes; i+=2) {
631  cArray[i] = fAllBits[i+1];
632  cArray[i+1] = fAllBits[i];
633  }
634 
635  if (nBytes>nSafeBytes) {
636  cArray[nSafeBytes+1] = fAllBits[nSafeBytes];
637  }
638 }
639 
640 void TBits::Get(Int_t *array) const
641 {
642  // Get all the bytes.
643 
644  UInt_t nBytes = (fNbits+7)>>3;
645  UInt_t nSafeBytes = nBytes&~3;
646 
647  UChar_t *cArray=(UChar_t*)array;
648  UInt_t i;
649  for (i=0; i<nSafeBytes; i+=4) {
650  cArray[i] = fAllBits[i+3];
651  cArray[i+1] = fAllBits[i+2];
652  cArray[i+2] = fAllBits[i+1];
653  cArray[i+3] = fAllBits[i];
654  }
655 
656  for (i=0; i<nBytes-nSafeBytes; ++i) {
657  cArray[nSafeBytes + (3 - i)] = fAllBits[nSafeBytes + i];
658  }
659 }
660 
661 void TBits::Get(Long64_t *array) const
662 {
663  // Get all the bytes.
664 
665  UInt_t nBytes = (fNbits+7)>>3;
666  UInt_t nSafeBytes = nBytes&~7;
667 
668  UChar_t *cArray=(UChar_t*)array;
669  UInt_t i;
670  for (i=0; i<nSafeBytes; i+=8) {
671  cArray[i] = fAllBits[i+7];
672  cArray[i+1] = fAllBits[i+6];
673  cArray[i+2] = fAllBits[i+5];
674  cArray[i+3] = fAllBits[i+4];
675  cArray[i+4] = fAllBits[i+3];
676  cArray[i+5] = fAllBits[i+2];
677  cArray[i+6] = fAllBits[i+1];
678  cArray[i+7] = fAllBits[i];
679  }
680 
681  for (i=0; i<nBytes-nSafeBytes; ++i) {
682  cArray[nSafeBytes + (7 - i)] = fAllBits[nSafeBytes + i];
683  }
684 }
685 
686 #endif
687 
688 Bool_t TBits::operator==(const TBits &other) const
689 {
690  // Compare object.
691 
692  if (fNbits == other.fNbits) {
693  return !memcmp(fAllBits, other.fAllBits, (fNbits+7)>>3);
694  } else if (fNbits < other.fNbits) {
695  return !memcmp(fAllBits, other.fAllBits, (fNbits+7)>>3) && other.FirstSetBit(fNbits) == other.fNbits;
696  } else {
697  return !memcmp(fAllBits, other.fAllBits, (other.fNbits+7)>>3) && FirstSetBit(other.fNbits) == fNbits;
698  }
699 }
700 
void Output(std::ostream &) const
Print the value to the std::ostream.
Definition: TBits.cxx:432
void Print(Option_t *option="") const
Print the list of active bits.
Definition: TBits.cxx:454
void Clear(Option_t *option="")
Clear the value.
Definition: TBits.cxx:84
long long Long64_t
Definition: RtypesCore.h:69
void Set(UInt_t nbits, const Char_t *array)
Set all the bytes.
Definition: TBits.cxx:492
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
const char Option_t
Definition: RtypesCore.h:62
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void DoRightShift(UInt_t shift)
Execute the left shift operation.
Definition: TBits.cxx:239
void ResetAllBits(Bool_t value=kFALSE)
Reset all bits to 0 (false).
Definition: TBits.cxx:470
TBits & operator=(const TBits &)
TBits assignment operator.
Definition: TBits.cxx:56
UChar_t * fAllBits
Definition: TBits.h:35
Bool_t operator==(const TBits &other) const
Definition: TBits.cxx:688
virtual ~TBits()
TBits destructor.
Definition: TBits.cxx:76
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
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:250
void Get(Char_t *array) const
Copy all the byes.
Definition: TBits.cxx:505
UInt_t fNbytes
Definition: TBits.h:34
UInt_t LastSetBit(UInt_t startBit=999999999) const
Return position of first non null bit (starting from position 0 and up)
Definition: TBits.cxx:393
UInt_t LastNullBit(UInt_t startBit=999999999) const
Return position of first null bit (starting from position 0 and up)
Definition: TBits.cxx:309
UInt_t FirstSetBit(UInt_t startBit=0) const
Return position of first non null bit (starting from position 0 and up)
Definition: TBits.cxx:348
void Compact()
Reduce the storage used by the object to a minimun.
Definition: TBits.cxx:95
unsigned int UInt_t
Definition: RtypesCore.h:42
short Short_t
Definition: RtypesCore.h:35
void DoLeftShift(UInt_t shift)
Execute the left shift operation.
Definition: TBits.cxx:216
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
#define ClassImp(name)
Definition: Rtypes.h:336
TBits(UInt_t nbits=8)
TBits constructor. All bits set to 0.
Definition: TBits.cxx:33
Mother of all ROOT objects.
Definition: TObject.h:37
Container of bits.
Definition: TBits.h:29
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:447
void DoFlip()
Execute ~(*this)
Definition: TBits.cxx:205
unsigned char UChar_t
Definition: RtypesCore.h:34
UInt_t CountBits(UInt_t startBit=0) const
Return number of bits set to 1 starting at bit startBit.
Definition: TBits.cxx:118
UInt_t fNbits
Definition: TBits.h:33
void ReserveBytes(UInt_t nbytes)
Reverse each bytes.
Definition: TBits.cxx:478
const Int_t n
Definition: legend1.C:16
UInt_t FirstNullBit(UInt_t startBit=0) const
Return position of first null bit (starting from position 0 and up)
Definition: TBits.cxx:264