www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C Bitfields in D

reply Jonathan M Davis <jmdavisProg gmx.com> writes:
Okay. I'm trying to get some C code to be properly callable from some D code, 
which naturally involves some extern(C) blocks. One of the types that I have to 
deal with looks something like this:

    typedef struct
    {
        unsigned i:1;
    } my_struct;

I have no idea how to do such a bitfield in D. Does a std.bitmanip.bitfield
work? 
I wouldn't have thought so, but I don't know. What would be the proper way to 
create a properly compatible struct in D?

- Jonathan M Davis
Dec 07 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Jonathan M Davis Wrote:

 Okay. I'm trying to get some C code to be properly callable from some D code, 
 which naturally involves some extern(C) blocks. One of the types that I have
to 
 deal with looks something like this:
 
     typedef struct
     {
         unsigned i:1;
     } my_struct;
 
 I have no idea how to do such a bitfield in D. Does a std.bitmanip.bitfield
work? 
 I wouldn't have thought so, but I don't know. What would be the proper way to 
 create a properly compatible struct in D?
 
 - Jonathan M Davis
I had some help from htod translating Fuse headers: https://github.com/he-the-great/Fused/blob/master/src/fuse_common.d#L47 struct my_struct { uint __bitfield1; property uint i() { return (__bitfield1 >> 0) & 0x1; } property uint i(uint value) { __bitfield1 = (__bitfield1 & 0xfffffffffffffffe) | (value << 0); return value; } } I think I got the copy/past/modify right.
Dec 07 2010