interfacing C bitfield structs

Walter Bright newshound at digitalmars.com
Sat Mar 18 11:26:36 PST 2006


"Regan Heath" <regan at netwin.co.nz> wrote in message 
news:ops6lu2fsx23k2f5 at nrage.netwin.co.nz...
> I don't know if you'd call this elegant but here is what I did. The 
> important thing is using a type which is the correct size for the 
> bitfields and then creating some way to interface the bits you need.
>
> struct DCB {
>     DWORD DCBlength;       /* sizeof(DCB)                     */
>     DWORD BaudRate;        /* Baudrate at which running       */
>     uint BITS;
>     //bit fBinary;           /* Binary Mode (skip EOF check)    */
>     //bit fParity;           /* Enable parity checking          */

Add two member functions for each:

    bool fBinary() { return BITS & 1; }
    bool fBinary(bool b) { BITS |= b; return b; }

    bool fParity() { return (BITS & 2) != 0; }
    bool fParity(bool b) { BITS |= b << 1; return b; }

and then, because functions work like properties, you can use them as if 
they were properties. 





More information about the Digitalmars-d mailing list