Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 08, 2005 structs and bits | ||||
---|---|---|---|---|
| ||||
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 |
June 08, 2005 Re: structs and bits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel F. Simoes | 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 Simoes
>
>
struct _name {
bit fieldA;
bit fieldB;
}
what about this? but i`m not sure..
|
June 08, 2005 Re: structs and bits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel F. Simoes | 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 |
June 08, 2005 Re: structs and bits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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 |
June 08, 2005 Re: structs and bits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote: <snip> > 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"... 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. |
June 08, 2005 Re: structs and bits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon wrote:
>> No, that does not work. Single bits are the size of "byte"...
>
> But _should_ it work? AIUI that's not indicated in the spec.
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?
--anders
|
June 08, 2005 Re: structs and bits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote: > Stewart Gordon wrote: > >>> No, that does not work. Single bits are the size of "byte"... >> >> But _should_ it work? AIUI that's not indicated in the spec. > > 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? 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. |
Copyright © 1999-2021 by the D Language Foundation