1 #ifndef CARMA_UTIL_BITTWIDDLING_H
2 #define CARMA_UTIL_BITTWIDDLING_H
7 #include "carma/util/compileTimeCheck.h"
14 template <
int highestBit,
int lowestBit >
15 unsigned char extractBitfield(
unsigned char x );
17 template <
int highestBit,
int lowestBit >
18 unsigned short extractBitfield(
unsigned short x );
20 template <
int highestBit,
int lowestBit >
21 unsigned int extractBitfield(
unsigned int x );
23 template <
int highestBit,
int lowestBit >
24 unsigned long extractBitfield(
unsigned long x );
26 template <
int highestBit,
int lowestBit >
27 unsigned long long extractBitfield(
unsigned long long x );
34 template <
int highestBit,
int lowestBit >
35 char extractBitfield(
char x );
37 template <
int highestBit,
int lowestBit >
38 short extractBitfield(
short x );
40 template <
int highestBit,
int lowestBit >
41 int extractBitfield(
int x );
43 template <
int highestBit,
int lowestBit >
44 long extractBitfield(
long x );
46 template <
int highestBit,
int lowestBit >
47 long long extractBitfield(
long long x );
52 template <
int highestBit,
int lowestBit >
53 float extractBitfield(
float x );
55 template <
int highestBit,
int lowestBit >
56 double extractBitfield(
double x );
58 template <
int highestBit,
int lowestBit >
59 long double extractBitfield(
long double x );
65 template <
int highestBit,
int lowestBit,
typename T >
66 T extractUnsignedBitfieldImpl( T x );
76 template <
int highestBit,
80 carma::util::detail::extractUnsignedBitfieldImpl(
const T x )
82 compileTimeCheck< (highestBit >= 0) >();
83 compileTimeCheck< (highestBit < (8 * sizeof( T ))) >();
85 compileTimeCheck< (lowestBit >= 0) >();
86 compileTimeCheck< (lowestBit < (8 * sizeof( T ))) >();
88 compileTimeCheck< (highestBit >= lowestBit) >();
90 const T hiMask = ~((~T(1)) << highestBit);
91 const T result = ((x & hiMask) >> lowestBit);
97 template <
int highestBit,
int lowestBit >
99 carma::util::extractBitfield(
const unsigned char x )
101 return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
105 template <
int highestBit,
int lowestBit >
106 inline unsigned short
107 carma::util::extractBitfield(
const unsigned short x )
109 return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
113 template <
int highestBit,
int lowestBit >
115 carma::util::extractBitfield(
const unsigned int x )
117 return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
121 template <
int highestBit,
int lowestBit >
123 carma::util::extractBitfield(
const unsigned long x )
125 return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
129 template <
int highestBit,
int lowestBit >
130 inline unsigned long long
131 carma::util::extractBitfield(
const unsigned long long x )
133 return detail::extractUnsignedBitfieldImpl< highestBit, lowestBit >( x );
137 template <
int highestBit,
int lowestBit >
139 carma::util::extractBitfield(
const char x )
141 compileTimeCheck< false >();
147 template <
int highestBit,
int lowestBit >
149 carma::util::extractBitfield(
const short x )
151 compileTimeCheck< false >();
157 template <
int highestBit,
int lowestBit >
159 carma::util::extractBitfield(
const int x )
161 compileTimeCheck< false >();
167 template <
int highestBit,
int lowestBit >
169 carma::util::extractBitfield(
const long x )
171 compileTimeCheck< false >();
177 template <
int highestBit,
int lowestBit >
179 carma::util::extractBitfield(
const long long x )
181 compileTimeCheck< false >();
187 template <
int highestBit,
int lowestBit >
189 carma::util::extractBitfield(
const float x )
191 compileTimeCheck< false >();
197 template <
int highestBit,
int lowestBit >
199 carma::util::extractBitfield(
const double x )
201 compileTimeCheck< false >();
207 template <
int highestBit,
int lowestBit >
209 carma::util::extractBitfield(
const long double x )
211 compileTimeCheck< false >();