ATLAS Offline Software
Loading...
Searching...
No Matches
BitField.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef TRK_BITFIELD_H
6#define TRK_BITFIELD_H
7
8namespace Trk {
9
13 template<class T>
14 class BitField {
15 public:
21 BitField( unsigned int offset_,unsigned int bits_ );
22
24 bool encode( unsigned int value, T& id ) const;
25
27 unsigned int decode( T id ) const;
28
29 private:
30 unsigned int m_offset;
31 unsigned int m_bits;
32 unsigned int m_maxValue;
33 unsigned int m_mask;
34 };
35
36 template< class T>
37 bool BitField<T>::encode( unsigned int value, T& id ) const {
38 // check that the value is in range
39 if( value >= m_maxValue ) { return false;}
40
41 // clear m_bits
42 id &= ~m_mask;
43
44 // set m_bits
45 id |= (value<<m_offset);
46
47 return true;
48 }
49
50 template< class T>
51 unsigned int BitField<T>::decode( T id ) const {
52 // apply m_mask and shift with m_offset
53 return (id & m_mask) >> m_offset;
54 }
55
56 template< class T>
57 BitField<T>::BitField( unsigned int offset_,unsigned int bits_ ) : m_offset(offset_),m_bits(bits_) {
58 // calculate m_mask
59 m_mask = 0;
60 for( unsigned int bit = m_offset; bit < m_offset + m_bits; ++bit ) {
61 m_mask |= (1<<bit);
62 }
63 // silly way of calculating 2^m_bits
64 m_maxValue = 1;
65 for( unsigned int i=0;i<m_bits;++i ) { m_maxValue *= 2;}
66 }
67}
68
69#endif
unsigned int decode(T id) const
returns the result of decode the input id
Definition BitField.h:51
unsigned int m_bits
position of the first bit manipulated by the BitField
Definition BitField.h:31
unsigned int m_maxValue
number of m_bits that the BitField is allowed to manipulate
Definition BitField.h:32
unsigned int m_mask
maximum allow value
Definition BitField.h:33
unsigned int m_offset
Definition BitField.h:30
BitField(unsigned int offset_, unsigned int bits_)
constructor, taking the offset (position of the first bit) and the number of bits No range checking i...
Definition BitField.h:57
bool encode(unsigned int value, T &id) const
encode a value into id, return false if the value is out of range
Definition BitField.h:37
Ensure that the ATLAS eigen extensions are properly loaded.