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

paulsunion 2012-10-08 01:02:54
最近在学习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;
}
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
paulsunion 2012-10-11
  • 打赏
  • 举报
回复
谢谢,对我非常有帮助。
qldsrx 2012-10-08
  • 打赏
  • 举报
回复
下面代码就是完美模拟C++中的union结构,改写了unBitConvert,其长度和C++中的完全相同,功能完全一样,
验证长度可以用sizeof函数输出查看。
    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;
}
}
}
}
qldsrx 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
那是PInvoke封送处理时用的,要在程序里直接计算还是BitArray比较合适:
[/Quote]
你这个是变通的实现,并不符合原意,因为在C++里也可以用bool数组的,但是为啥用联合体?
另外你再看看他的问题,他问的不是那个stBitType,而是那个unBitConvert
union unBitConvert
{
struct stBitType BitValue;
unsigned char ucValue;
};
iyomumx 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

FieldOffset特性
[/Quote]
那是PInvoke封送处理时用的,要在程序里直接计算还是BitArray比较合适:

static byte modMult02(byte x)
{
byte[] t = new byte[] { x };
BitArray input = new BitArray(t);
BitArray output = new BitArray(8);
output[0] = input[7];
output[7] = input[6];
output[6] = input[5];
output[5] = input[4];
output[2] = input[1];
output[1] = input[0] ^ input[7];
output[3] = input[2] ^ input[7];
output[4] = input[3] ^ input[7];
output.CopyTo(t, 0);
return t[0];
}
浪子-无悔 2012-10-08
  • 打赏
  • 举报
回复
这个还真没接触过,友情帮顶吧~~~
bdmh 2012-10-08
  • 打赏
  • 举报
回复
FieldOffset特性

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧