Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 15, 2009 8:10 AM
Hi! i'm writing an application (also converting some codes from C++ to C#) that needs to have a 1 Byte Struct with 8 fields, which means that each field is 1 bit. I know that in C++ i can declare the variable as (public int x:1) but i can't in C#. I get the idea of using structs instead of Unions but still i can't get the idea of declaring variables as bitfields. I tried to use FieldOffset attribute but either i can get them in 0 position or 1st position which also means a byte. I can overlap the variables but this time i can only use one of them. So please any ideas, any hints or leads to get me to the solution. i'm posting a code sniplet in case i couldn't make myself clear;
"i have this in C++;
typedef union
{
struct
{
unsigned StoredUser :1;
unsigned SettingAllDiger :1;
unsigned SettingAllBolgeler :1;
unsigned AnaLabel :1;
unsigned ExtLabelAllExt1 :1;
unsigned ExtLabelAllExt2 :1;
unsigned None :2;
};
}VeriOkumaStatus_t;
and tried to convert to C#like this;
[StructLayout(LayoutKind.Explicit,Size= 1)]
struct VeriOkumaStatus_t
{
[FieldOffset(0)]
public byte StoredUser;
[FieldOffset(0)]
public byte SettingAllDiger;
[FieldOffset(0)]
public byte SettingAllBolgeler;
[FieldOffset(0)]
public byte AnaLabel;
[FieldOffset(0)]
public byte ExtLabelAllExt1;
[FieldOffset(0)]
public byte ExtLabelAllExt2;
[FieldOffset(0)]
public byte None;
}
All replies (2)
Wednesday, July 15, 2009 8:26 AM | 2 votes
Hi there,
You might want to have a look at the following link.
http://stackoverflow.com/questions/14464/bit-fields-in-c
Regards
Wednesday, July 15, 2009 8:36 AM
Thanx IvanRSA for the quick reply. I better focus at that, seems like what i've been trying to learn. Regards. Do It Your Way!