Would anyone know how to extract the size of a bit-field member. The below code naturally gives me the size of an integer, but how do I find out how many bits or b
Here is a little bit tricky generalized version:
#include
#include
#include
#include
using namespace std;
template
T umaxof()
{
T t;
memset(&t, 0xFF, sizeof(T));
return t;
}
template
size_t bitsof(const T& umax)
{
return bitset(umax).count();
}
int main()
{
struct A
{
uint32_t bf1:19;
uint32_t bf2:1;
};
cout << bitsof(umaxof().bf1) << "\n";
cout << bitsof(umaxof().bf2) << "\n";
return 0;
}
It can be tried out at https://ideone.com/v4BiBH
Note: Works with unsigned bit fields only.