interfacing C bitfield structs

Wolfgang Draxinger wdraxinger at darkstargames.de
Sat Mar 18 15:35:59 PST 2006


Walter Bright wrote:

> 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.

These set functions won't work if b==0, since it's just an OR.
What's needed is 

BITS = (BITS & ~(1<<n)) | b ? 1<<n : 0 ;

Anyways that's a cool idea to do it. Just looks a bit strange.

-- 
Wolfgang Draxinger




More information about the Digitalmars-d mailing list