他の質問に答えるには、C#では構造体が必要になる可能性があり、属性を使用してユニオンのような動作を引き出す必要があります。
この特定の例は、次のようになります。
[StructLayout(LayoutKind.Explicit)]
struct uAWord {
[FieldOffset(0)]
private uint theWord = 0;
[FieldOffset(0)]
public int m_P;
[FieldOffset(1)]
public int m_S;
[FieldOffset(3)]
public int m_SS;
[FieldOffset(7)]
public int m_O;
[FieldOffset(18)]
public int m_D;
public uAWord(uint theWord){
this.theWord = theWord;
}
}
はLayoutKind.Explicit
、メモリ内のどこに各フィールドをマップするかを指示し、各FieldOffset(int)
フィールドを開始するビットを指示することを示します。 詳細については、こちらをご覧ください。 コンストラクターでを設定してこの構造体を割り当てるuint theWord
と、他の各プロパティが異なるメモリアドレスで始まるチャンクにアクセスします。
残念ながら、それは実際には正しくありません。プロパティを使用し、ビットマスキング/シフトを実行して正しく設定する必要があります。このような:
struct uAWord {
private uint theWord = 0;
public int m_P {get {return (theWord & 0x01);}}
public int m_S {get {return (theWord & 0x02) << 2;}}
public int m_SS {get {return (theWord & 0x04) << 3;}}
public int m_0 {get {return (theWord & 0x18) << 6;}}
}