日期:2014-05-17  浏览次数:20926 次

关于c#实现c++一样的位寻址
最近在学习c#,但是遇到一个union的问题,之前有一段代码是c++的,如下,怎样在c#中实现这种功能呢?望高人指点啊,不甚感激。
struct stBitType
{
unsigned int Bit0 :1;
unsigned int Bit1 :1;
unsigned int Bit2 :1;
unsigned int Bit3 :1;
unsigned int Bit4 :1;
unsigned int Bit5 :1;
unsigned int Bit6 :1;
unsigned int Bit7 :1;
};
union unBitConvert
{
struct stBitType BitValue;
unsigned char ucValue;
};
unsigned char modMult02(unsigned char x)
{
union unBitConvert Input,Output;
Input.ucValue=x;

Output.BitValue.Bit0=Input.BitValue.Bit7;
Output.BitValue.Bit7=Input.BitValue.Bit6;
Output.BitValue.Bit6=Input.BitValue.Bit5;
Output.BitValue.Bit5=Input.BitValue.Bit4;
Output.BitValue.Bit2=Input.BitValue.Bit1;

Output.BitValue.Bit1=Input.BitValue.Bit0^Input.BitValue.Bit7;
Output.BitValue.Bit3=Input.BitValue.Bit2^Input.BitValue.Bit7;
Output.BitValue.Bit4=Input.BitValue.Bit3^Input.BitValue.Bit7;

return Output.ucValue;
}

------解决方案--------------------
探讨

FieldOffset特性

------解决方案--------------------
下面代码就是完美模拟C++中的union结构,改写了unBitConvert,其长度和C++中的完全相同,功能完全一样,
验证长度可以用sizeof函数输出查看。
C# code
    public struct unBitConvert
    {
        int innerValue;

        public byte ucValue
        {
            get { return (byte)(innerValue & 0xff); }
            set { innerValue = value; }
        }

        public bool Bit0
        {
            get
            {
                return (innerValue & 1) != 0;
            }
            set
            {
                if (value)
                {
                    innerValue |= 1;
                }
                else
                {
                    innerValue &= ~1;
                }
            }
        }
        public bool Bit1
        {
            get
            {
                return (innerValue & 2) != 0;
            }
            set
            {
                if (value)
                {
                    innerValue |= 2;
                }
                else
                {
                    innerValue &= ~2;
                }
            }
        }
        public bool Bit2
        {
            get
            {
                return (innerValue & 4) != 0;
            }
            set
            {
                if (value)
                {
                    innerValue |= 4;
                }
                else
                {
                    innerValue &= ~4;
                }
            }
        }
        public bool Bit3
        {
            get
            {
                return (innerValue & 8) != 0;
            }
            set
            {
                if (value)
                {
                    innerValue |= 8;
                }
                else
                {
                    innerValue &= ~8;
                }
            }
        }
        public bool Bit4
        {
            get
            {
                return (innerValue & 16) != 0;
            }
            set
            {
                if (value)
                {
                    innerValue |= 16;
                }
                else
                {
                    innerValue &= ~16;
                }
            }
        }
        public bool Bit5
        {
            get
            {
                return (innerValue & 32) != 0;
            }
            set
            {
                if (value)
                {
                    innerValue |= 32;
                }
                else
                {
                    innerValue &= ~32;
                }
            }
        }
        public bool Bit6
        {
            get
            {
                return (in