2

I try to get the size of a bitfield.

For example, I have got a generic handle:

template<size_t n, size_t m>
struct handle
{
    uint32 index : n; 
    uint32 validation : m;
}

Now I want to get the size of the members.

I found a macro that works when I have a handle<16, 16> and expands the desired members to sizeof. In this case, if I pass in the index members I get 16 as my output.

But there I would have to pass in my output variable.

Is there a way maybe with some template magic to expand directly to the desired number? So I could pass in sizeof_bit(class, member) and I get the sizebit size of this member?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • sizeof(uint32) or sizeof(n) will get you the size of the member in bytes (8 bits per byte). Though you explicitly define them as 32 bits. so I'm guessing you have a different question? – IdeaHat Jun 11 '14 at 17:25
  • 1
    @MadScienceDreams: I think he wants to know the value of `n` and `m`, i.e. how many bits per bitfield. (``You're not guaranteed to have exactly 8 bits per byte.``) – Lstor Jun 11 '14 at 17:27
  • 2
    Maybe you should share the macro that works, and identify the source of the information. There most likely isn't a way to recover the sizes of the bit-fields via `sizeof()` et al. You probably have to capture them as part of the type -- non-bitfield members that store `n` and `m` and are initialized by the constructors. – Jonathan Leffler Jun 11 '14 at 17:29
  • 2
    @JonathanLeffler agree with general approach, but it would be more common to store them as static members, avoiding both the per-object memory usage and construction cost.... – Tony Delroy Jun 11 '14 at 17:41
  • @MadScienceDreams You can't `sizeof` a bitfield. – T.C. Jun 11 '14 at 20:21
  • @TonyD: I knew there was a reason to make it a comment rather than an answer...You're right, of course. – Jonathan Leffler Jun 11 '14 at 20:51

1 Answers1

3

Maybe something like

template<size_t n, size_t m>
struct handle
{
    enum { index_bits = n };
    enum { validation_bits = m };
    uint32_t index : n; 
    uint32_t validation : m;
};

Demo.

If you have no control over the structure defining the bitfield, you can do something like this to count the number of bits in it at run time, but it's fairly inefficient.

Community
  • 1
  • 1
T.C.
  • 133,968
  • 17
  • 288
  • 421
  • the macro is this #define SIZEOF_BITFIELD(class,member,out) { \ class tmp_; \ tmp_.member = ~0; \ unsigned int tmp2_ = tmp_.member; \ ++tmp2_; \ out = log2(tmp2_); \ } from http://stackoverflow.com/questions/539251/getting-the-size-of-an-indiviual-field-from-a-c-struct-field/539307#539307 for sure i could save the number of bits in the handle but then storing it everytime in a handle would be very inefficient :( – Alexander Braunreuther Jun 11 '14 at 23:22
  • 1
    @user3731014 In the code above, the `enum` members (`index_bits`, etc.) are compile-time constants and are not actually stored in `handle` objects. That's why GCC produces a "variable set but not used" warning in the linked demo code. – T.C. Jun 11 '14 at 23:35
  • Okay, but can i use them in another class without overhead? Ex.: template struct anything { int anarray[2 ^ handletype:: index_bits] } – Alexander Braunreuther Jun 12 '14 at 01:43
  • 1
    @user3731014 Yes (you probably don't want to use `^` though, that's bitwise XOR). – T.C. Jun 12 '14 at 02:06
  • Okay, sounds nice. Thanks! (Ah yeah you're right.. Got a bit late.) – Alexander Braunreuther Jun 12 '14 at 02:19