arbitrary bitsize of variables

Frank Benoit keinfarbton at googlemail.com
Sat Sep 29 07:46:21 PDT 2007


dominik schrieb:
> Is it possible in D to do something like this (in C++):
> 
> struct Testor {
>      int small_int: 5;
>      int even_smaller_int: 3;
>  };
> 
> this basically says make one integer of 5 bit size, and other of 3 bit 
> size.. whole structure is 8bits long 
> 
> 


D does not support bitfields. You can use property methods to get/set
bits. They can later be inlined by the compiler.

struct Testor {
  int data;
  int  small_int(){ return data & 0x1f; }
  void small_int( int val ){ data = val | (data & ~0x1f); }
...
};

Tricks with templates and mixins are also possible.


More information about the Digitalmars-d-learn mailing list