Representing merged bit fields using C # StrucLayout and FieldOffset

I understand that to represent joins in C # I need to use StructLayout [LayoutKind.Explicit)] and [FieldOffset (x)] to indicate the byte offset within the union. However, I have the following union that I want to introduce, and FieldOffset only assigns an offset in byte size.

union _myUnion { unsigned int info; struct { unsigned int flag1:1 // bit 0 unsigned int flag2:1 // bit 1 unsigned int flag3:1 // bit 2 unsigned int flag4:1 // bit 3 unsigned int flag5:1 // bit 4 unsigned int flag6:1 // bit 5 . . . unsigned int flag31:1 // bit 31 } } 

As you can see for the internal structure in the union, I cannot use FieldOffset, since I need something that can be compensated a bit.

Is there a solution? I am trying to call a DLL function, and one of the struct data was defined as such, and I had no idea how to best represent this union structure.

+4
source share
1 answer

No need for pooling; one property of the + field for data, 8 properties that perform bitwise "shift" operations, for example:

 public uint Value {get;set;} public uint Flag2 { get { return Value >> 2; } } 

etc .. I would also think that you want a bool here?

Normally I would say: do not make volatile structures. PInvoke may (I'm not sure) be a valid script for this, so I will ignore it :)

If the value does use more than 32 bits, consider switching the padding field to ulong.

+3
source

Source: https://habr.com/ru/post/1337505/


All Articles