Bitfield structs and suggestion

Regan Heath regan at netmail.co.nz
Fri Nov 23 01:06:56 PST 2007


Daniel Keep wrote:
> D originally had a 'bit' type in place of 'bool' which could be packed
> in structs.  However, people didn't think very much of it, and fairly
> loudly complained that they wanted a "real" bool type.  Hence, 'bit' is
> now aliased to 'bool'.
> 
> The other problem was that 'bit' introduced various problems.  For
> instance, you can take the address of anything *except* a bitfield,
> since addresses only have byte-level granularity.  Bitfields also cannot
> have sensible sizeof properties, which could break generic code.
> 
> Off the top of my head, maybe it would suffice to give types a special
> 'bits' slice type.
> 
> int some_float;
> int negative = some_float.bits[0..1];
> int exponent = some_float.bits[1..9];
> int mantissa = some_float.bits[9..$];
> 
> Basically just tarting up manual shifts and masking.  Just a thought :)

You're not the first to think so :)

In fact, why not just slice the int directly:

int one;
int two;

one[0..4] = two[4..8];
one[4..8] = two[0..4];

In the meantime, in order to avoid having to re-code all the bit shifts 
in every struct with bitfields isn't there a mixin which could implement 
properties for accessing specific bits.

Regan



More information about the Digitalmars-d mailing list