bitfields

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Jun 18 06:58:42 PDT 2009


On Thu, Jun 18, 2009 at 9:16 AM, novice2<sorry at noem.ail> wrote:
> does anyone know: is D2 std.bitmanip compatible with C bitfields?
> can i use it, if i need translate .h file with something like this:
>
> typedef struct {
>    unsigned int can_compress : 1;
>    unsigned int can_uncompress : 1;
>    unsigned int can_get_info : 1;
>    unsigned int : 7;
>    unsigned int : 16;
>    unsigned int : 16;
>    unsigned int : 16;
>    unsigned int : 16;
>    unsigned int : 16;
> } capabilities;
>
> Thanks.

Theoretically speaking, it's not possible to answer your question
because the ordering, padding, and arrangement of bitfields in C is
not defined.  It's entirely possible to have two different C compilers
output different code for the same bitfield definitions.  For that
reason, it may not be possible to interface with the corresponding C
library with any other language than C or any other compiler than the
one it was compiled with.

Practically speaking, sure.  Most C compilers will take the path of
least resistance and just pack all the bitfields together as tightly
as they can starting with the LSB, and that is - AFAIK - what
std.bitmanip's bitfield support does.  But there are no guarantees.


More information about the Digitalmars-d-learn mailing list