Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gommo

Bit addressing in structures in C#

This topic is 5329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just wondering if you can do the equivalent of the following in C# as you can do in C/C++??
struct blah 
{
    unsigned type:4;
    unsigned someother:5;
    unsigned yep:7;
};

Share this post


Link to post
Share on other sites
Advertisement
If I understand what you''re asking, then yes. If you mean can you manually specify the byte offset for each field within a struct, then yes you can.

Something like this:


using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit)]
public struct Test
{
[FieldOffset(4)]
uint type;

[FieldOffset(5)]
uint someother;

[FieldOffset(7)]
uint yep;
}



Tim

Share this post


Link to post
Share on other sites
The FieldOffset attribute specifies byte offsets, not bit widths. I''m not aware of any mechanism in C# that will specify bits analagous to the example given.

Share this post


Link to post
Share on other sites
I looking @ loading in information from a save game file. It is saved (using c++) as compact as possible, so it uses just bits to represent things. So if there were only say 5 types of an item we''d use only three bits.

I''m thinking of writing an easy editor for it in C# or something. (Mainly due to the ease of GUI design) But I may just have to do it in MFC.

Share this post


Link to post
Share on other sites
roastedamoeba - he wants to do it for the same reason InteropServices exist in the first place ... cause other langauges do it .. and he needs to interop

Share this post


Link to post
Share on other sites
Well, for interop-purposes I'd do it so: let the struct have private members of the size as the C++-struct's size and provide properties that do the bit-fiddling. Say, the total size is like 16 bits, and you want provides the 3 values you showed in your example, then what about:


[StructLayout(LayoutKind.Sequential, Size=2, Pack=2)]
struct MyBlah
{
private Int16 data;
public uint Type
{
get { return data & 0xf; }
set { data = (data & 0xfff0) | value & 0xf; }
}
public uint SomeOther
{
get { return data & 0x1f0; }
set { data = (data & 0xfe0f) | value & 0x1f0; }
}
public uint Yep
{
get { return data & 0xfe00; }
set { data = (data & 0x1ff) | value & 0xfe00; }
}
}


Regards,
Andre Loker

[edit]sry, clicked the button to soon[/edit]


[edited by - VizOne on May 12, 2004 3:09:46 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by VizOne

[StructLayout(LayoutKind.Sequential, Size=2, Pack=2)]
struct MyBlah
{
private Int16 data;
public uint Type
{
get { return data & 0xf; }
set { data = (data & 0xfff0) | value & 0xf; }
}
public uint SomeOther
{
get { return data & 0x1f0; }
set { data = (data & 0xfe0f) | value & 0x1f0; }
}
public uint Yep
{
get { return data & 0xfe00; }
set { data = (data & 0x1ff) | value & 0xfe00; }
}
}



You'd need to bit-shift the values so they were relative to 0.

Archimage
(Ann-Marie Ratcliffe)

[edited by - Archimage on May 12, 2004 3:17:01 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!