digitalmars.D.learn - structs and bits
- Miguel F. Simoes (10/10) Jun 07 2005 How can I emulate in D this C code?
- Phoenix (6/21) Jun 08 2005 struct _name {
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (15/30) Jun 08 2005 struct name
- Miguel F. Simoes (5/35) Jun 08 2005 Ok. Thanks. I will try.
- Stewart Gordon (8/18) Jun 08 2005 But _should_ it work? AIUI that's not indicated in the spec.
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (7/10) Jun 08 2005 Beats me, the "bit" type is lame anyway :-)
- Stewart Gordon (20/33) Jun 08 2005 Yes. But that isn't really necessary. How about making them only as
How can I emulate in D this C code? (I don't know the right way to convert the numbers after the colon) typedef struct _name { DWORD fieldA :1 DWORD fieldB :1 ... ... } name; Thanks Miguel Ferreira Simoes
Jun 07 2005
Miguel F. Simoes napsal(a):How can I emulate in D this C code? (I don't know the right way to convert the numbers after the colon) typedef struct _name { DWORD fieldA :1 DWORD fieldB :1 ... ... } name; Thanks Miguel Ferreira Simoesstruct _name { bit fieldA; bit fieldB; } what about this? but i`m not sure..
Jun 08 2005
Miguel F. Simoes wrote:How can I emulate in D this C code? (I don't know the right way to convert the numbers after the colon) typedef struct _name { DWORD fieldA :1 DWORD fieldB :1 ... ... } name;struct name { uint field; bit fieldA() { return cast(bit) (field >> 31) & 1; } void fieldA(bit b) { field = (field &~ 1<<31) | (b << 31); } bit fieldB() { return cast(bit) (field >> 30) & 1; } void fieldB(bit b) { field = (field &~ 1<<30) | (b << 30); } } Something like that... Not sure if the C bit order is defined ? (so you might need some kind of bitsex versioning in D as well) Phoenix wrote:struct _name { bit fieldA; bit fieldB; } what about this? but i`m not sure..No, that does not work. Single bits are the size of "byte"... (bit[32] might, but then only for even multiples of 32 bits) --anders
Jun 08 2005
Ok. Thanks. I will try. My goal is just to port the DCB windows struct to D. Miguel "Anders F Björklund" <afb algonet.se> escreveu na mensagem news:d86j62$1hsq$1 digitaldaemon.com...Miguel F. Simoes wrote:How can I emulate in D this C code? (I don't know the right way to convert the numbers after the colon) typedef struct _name { DWORD fieldA :1 DWORD fieldB :1 ... ... } name;struct name { uint field; bit fieldA() { return cast(bit) (field >> 31) & 1; } void fieldA(bit b) { field = (field &~ 1<<31) | (b << 31); } bit fieldB() { return cast(bit) (field >> 30) & 1; } void fieldB(bit b) { field = (field &~ 1<<30) | (b << 30); } } Something like that... Not sure if the C bit order is defined ? (so you might need some kind of bitsex versioning in D as well) Phoenix wrote:struct _name { bit fieldA; bit fieldB; } what about this? but i`m not sure..No, that does not work. Single bits are the size of "byte"... (bit[32] might, but then only for even multiples of 32 bits) --anders
Jun 08 2005
Anders F Björklund wrote: <snip>Phoenix wrote:But _should_ it work? AIUI that's not indicated in the spec. http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/10198 Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.struct _name { bit fieldA; bit fieldB; } what about this? but i`m not sure..No, that does not work. Single bits are the size of "byte"...
Jun 08 2005
Stewart Gordon wrote:Beats me, the "bit" type is lame anyway :-) I think it will have to occupy (at least) one byte, in order to be addressable ? (for use as pointer targets or inout parameters) Maybe the compiler could concatenate single bits next to each other into bit arrays or something. But then those are 32-bit aligned now? --andersNo, that does not work. Single bits are the size of "byte"...But _should_ it work? AIUI that's not indicated in the spec.
Jun 08 2005
Anders F Björklund wrote:Stewart Gordon wrote:Yes. But that isn't really necessary. How about making them only as many bytes as necessary, and obeying the alignment setting? Even if there would be a performance hit: - it would at least make it possible to use bit arrays to wrap bit flags in structs - instances that aren't in structs, or happen to be 32-bit aligned, can still use the efficient implementation for this case. I guess a good course of action would be to make this change to bit arrays, and to have consecutive bit members of a struct implemented as a bit array internally. Of course they won't be addressable, but that's a necessary side effect under the current constraints. And if people want to use bytes instead, then they can. We'd also need to consider what happens to endianness from a portability POV. FTM I just tested it with gdc on Mac OS X - a big-endian platform but bit arrays seem to be little-endian nonetheless (albeit left-aligned). Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.Beats me, the "bit" type is lame anyway :-) I think it will have to occupy (at least) one byte, in order to be addressable ? (for use as pointer targets or inout parameters) Maybe the compiler could concatenate single bits next to each other into bit arrays or something. But then those are 32-bit aligned now?No, that does not work. Single bits are the size of "byte"...But _should_ it work? AIUI that's not indicated in the spec.
Jun 08 2005