Does C# support bit fields (bit packing) in structure?

Posted by Pbshines under C# on 9/20/2013 | Points: 10 | Views : 3449 | Status : [Member] | Replies : 3
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#?

Shine.PB


Responses

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can see the below link:-
http://stackoverflow.com/questions/14464/bit-fields-in-c-sharp

Happy Coding

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Pbshines, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Also you can do this with
Properties or
Enum

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER


Pbshines, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
I'd probably knock together something using attributes, then a conversion class to convert suitably attributed structures to the bitfield primitives. Something like...
using System;


namespace BitfieldTest
{
[global::System.AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
sealed class BitfieldLengthAttribute : Attribute
{
uint length;

public BitfieldLengthAttribute(uint length)
{
this.length = length;
}

public uint Length { get { return length; } }
}

static class PrimitiveConversion
{
public static long ToLong<T>(T t) where T : struct
{
long r = 0;
int offset = 0;

// For every field suitably attributed with a BitfieldLength
foreach (System.Reflection.FieldInfo f in t.GetType().GetFields())
{
object[] attrs = f.GetCustomAttributes(typeof(BitfieldLengthAttribute), false);
if (attrs.Length == 1)
{
uint fieldLength = ((BitfieldLengthAttribute)attrs[0]).Length;

// Calculate a bitmask of the desired length
long mask = 0;
for (int i = 0; i < fieldLength; i++)
mask |= 1 << i;

r |= ((UInt32)f.GetValue(t) & mask) << offset;

offset += (int)fieldLength;
}
}

return r;
}
}

struct PESHeader
{
[BitfieldLength(2)]
public uint reserved;
[BitfieldLength(2)]
public uint scrambling_control;
[BitfieldLength(1)]
public uint priority;
[BitfieldLength(1)]
public uint data_alignment_indicator;
[BitfieldLength(1)]
public uint copyright;
[BitfieldLength(1)]
public uint original_or_copy;
};

public class MainClass
{
public static void Main(string[] args)
{
PESHeader p = new PESHeader();

p.reserved = 3;
p.scrambling_control = 2;
p.data_alignment_indicator = 1;

long l = PrimitiveConversion.ToLong(p);


for (int i = 63; i >= 0; i--)
{
Console.Write( ((l & (1l << i)) > 0) ? "1" : "0");
}

Console.WriteLine();

return;
}
}
}

Which produces the expected ...000101011. Of course, it needs more error checking and a slightly saner typing, but the concept is (I think) sound, reusable, and lets you knock out easily maintained structures by the dozen.

Reference: http://www.codeproject.com/Questions/276153/help-for-bit-field-in-csharp

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pbshines, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response