bit_manipulation.h
Go to the documentation of this file.
1 #ifndef cetlib_bit_manipulation_h
2 #define cetlib_bit_manipulation_h
3 
4 // ======================================================================
5 //
6 // bit_manipulation: Compile-time bit manipulations
7 //
8 // ======================================================================
9 
10 #include <cstddef>
11 #include <limits>
12 #include <type_traits>
13 
14 namespace cet {
15 
16  /// struct bit_size<U>.
18  struct bit_size;
19 
20  template <class U>
21  struct bit_size<U, true> {
22  static constexpr std::size_t value = std::numeric_limits<U>::digits;
23  };
24 
25  /// struct bit_number<U, n>.
26  template <class U,
27  std::size_t n,
28  bool = n<bit_size<U>::value> struct bit_number;
29 
30  template <class U, std::size_t n>
31  struct bit_number<U, n, true> {
32  static constexpr std::size_t value = U(1u) << n;
33  };
34 
35  template <class U, std::size_t n>
36  struct bit_number<U, n, false> {
37  static constexpr std::size_t value = U(0u);
38  };
39 
40  /// struct right_bits<U, n>.
41  template <class U,
42  std::size_t n,
44  bool = (n + 1) < bit_size<U>::value>
45  struct right_bits;
46 
47  template <class U, std::size_t n>
48  struct right_bits<U, n, true, true> {
49  static constexpr U value = bit_number<U, n + 1>::value - static_cast<U>(1u);
50  };
51 
52  template <class U, std::size_t n>
53  struct right_bits<U, n, true, false> {
54  static constexpr U value = ~0u;
55  };
56 
57  // struct left_bits<U, n>.
58  template <class U,
59  std::size_t n,
62  struct left_bits;
63 
64  template <class U, std::size_t n>
65  struct left_bits<U, n, true, true> {
66  private:
67  static constexpr U n_zeros = bit_size<U>::value - n;
68 
69  public:
70  static constexpr U value = ~right_bits<U, n_zeros>::value;
71  };
72 
73  template <class U, std::size_t n>
74  struct left_bits<U, n, true, false> {
75  static constexpr U value = U(-1);
76  };
77 
78  // U circ_lshift<U>().
79  template <class U>
81  circ_lshift(U X, U n)
82  {
83  constexpr std::size_t nbits = bit_size<U>::value;
84  constexpr std::size_t mask = nbits - 1ul;
85  n %= nbits;
86  return (X << n) | (X >> (nbits - n) & mask);
87  }
88 }
89 #endif /* cetlib_bit_manipulation_h */
90 
91 // Local Variables:
92 // mode: c++
93 // End:
const XML_Char int const XML_Char * value
Definition: expat.h:331
struct bit_size<U>.
Float_t X
Definition: plot.C:38