2

I think bit fields are not possible in c# structure but it is possible in c++. For Example:

typedef struct
{

    UINT16 SrcPort:16;
    UINT16 DstPort:16;
    UINT32 SeqNum:32;
    UINT32 AckNum:16;
    UINT16 Reserved1:4;
    UINT16 HdrLength:4;

}IP_HDR

How can we create a structure like this in c# with bit fields?

My question is how we can set bit fields in C# structure.Is there any ways to do it in C#?

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
pbshines
  • 21
  • 2
  • 2
    Might be related (not knowledgeable of the topic enough to know for sure): http://stackoverflow.com/questions/9311478/marshalling-stucts-with-bit-fields-in-c-sharp?rq=1 – Tim M. Sep 20 '13 at 06:18
  • This might also help: [Bit fields in C#](http://stackoverflow.com/questions/14464/bit-fields-in-c-sharp) – Jost Sep 20 '13 at 06:43

1 Answers1

3

No, C# does not offer any language features to define or access data storage smaller than 1 byte. You will have to mask and shift to isolate bit subranges within the data.

dthorpe
  • 35,318
  • 5
  • 75
  • 119