首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于c#兑现c++一样的位寻址

2012-10-18 
关于c#实现c++一样的位寻址最近在学习c#,但是遇到一个union的问题,之前有一段代码是c++的,如下,怎样在c#中

关于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 (innerValue & 64) != 0;            }            set            {                if (value)                {                    innerValue |= 64;                }                else                {                    innerValue &= ~64;                }            }        }        public bool Bit7        {            get            {                return (innerValue & 128) != 0;            }            set            {                if (value)                {                    innerValue |= 128;                }                else                {                    innerValue &= ~128;                }            }        }    } 

热点排行