10

I need to translate a C struct to C# which uses bit fields.

typedef struct foo { unsigned int bar1 : 1; unsigned int bar2 : 2; unsigned int bar3 : 3; unsigned int bar4 : 4; unsigned int bar5 : 5; unsigned int bar6 : 6; unsigned int bar7 : 7; ... unsigned int bar32 : 32; } foo; 

Anyone knows how to do this please?

7
  • 3
    Already answered in Bit fields in C#. Commented Feb 25, 2011 at 10:17
  • 2
    I hope you are aware of that you are allocating 1+2+3.. +32 bits = 528 bits = 66 bytes. Commented Feb 25, 2011 at 11:53
  • 1
    @Lundin: It is just an example, I just wanted to point out that I have all possible bit field variants. Commented Feb 25, 2011 at 12:08
  • @Aasmund Eldhuset: Yes I have already seen this post but I don't get it why he converts all uints into one long. Commented Feb 25, 2011 at 12:11
  • 1
    Are you trying to marshal the data? If not, you could just write your own BitField class which would be very simple. Commented Feb 25, 2011 at 17:16

4 Answers 4

8

As explained in this answer and this MSDN article, you may be looking for the following instead of a BitField

[Flags] enum Foo { bar0 = 0, bar1 = 1, bar2 = 2, bar3 = 4, bar4 = 8, ... } 

as that can be a bit annoying to calculate out to 232, you can also do this:

[Flags] enum Foo { bar0 = 0, bar1 = 1 << 0, bar2 = 1 << 1, bar3 = 1 << 2, bar4 = 1 << 3, ... } 

And you can access your flags as you would expect in C:

Foo myFoo |= Foo.bar4; 

and C# in .NET 4 throws you a bone with the HasFlag() method.

if( myFoo.HasFlag(Foo.bar4) ) ... 
Sign up to request clarification or add additional context in comments.

Comments

3

You could use the BitArray class for the framework. Look at the msdn article.

1 Comment

Thanks, but that would be pain in the ass if I have to declare every single bit.
-1

Unfortunately there is no such thing in C#. The closest thing is applying a StructLayout attribute and using FieldOffset attribute on fields. However the field offset is in bytes, not in bits. Here is an example:

[StructLayout(LayoutKind.Explicit)] struct MyStruct { [FieldOffset(0)] public int Foo; // this field's offset is 0 bytes [FieldOffset(2)] public int Bar; // this field's offset is 2 bytes. It overlaps with Foo. } 

But it is NOT the same as the functionality you want.

If what you need is to decode a bits sequence to a struct you will have to write manual code (using, for example, using classes like MemoryStream or BitConverter).

1 Comment

>If what you need is to decode a bits sequence to a struct you will have to write manual code (using, for example, using classes like MemoryStream or BitConverter). Well that was what I was looking for.
-2

Are you looking for the FieldOffset attribute? See here: FieldOffsetAttribute Class

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.