积极答复者
BitArray 来设置byte[]数组中byte[0]中的第2位,如何实现byte[0]的值得相应修改。

问题
-
byte[] mybyte = new byte[3] { 1, 2, 3 }; Console.WriteLine("1.byte[0]:{0}", mybyte[0]);//原始byte数据 BitArray myBitArray = new BitArray(mybyte); myBitArray.Set(3,myBitArray.Get(0));//把第3位的值设为第1位相同的值:1。 Console.WriteLine("2.byte[0]:{0}", mybyte[0]); Console.ReadLine();
显示结果:1.byte[0]:1
2.byte[0]:1
疑问:我查看myBitArray中的值,发现第三位已经成功修改位了“true”。
但是,在byte[]数组中的对于的值byte[0]没有被修改。
请问:如何使得byte[]数组对应的位被修改后,byte[]也相应被修改?
还原魔方
答案
-
byte[] mybyte = new byte[3] { 1, 2, 3 }; Console.WriteLine("1.byte[0]:{0}", mybyte[0]); BitArray myBitArray = new BitArray(mybyte); myBitArray.Set(3, myBitArray.Get(0)); myBitArray.CopyTo(mybyte, 0); Console.WriteLine("2.byte[0]:{0}", mybyte[0]); Console.ReadLine();
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已标记为答案 Demi_China 2013年3月18日 13:41
全部回复
-
构造函数public BitArray(byte[] bytes); 的注释:
// Initializes a new instance of the System.Collections.BitArray class that
// contains bit values copied from the specified array of bytes.可见,是拷贝bytes的,所以你改变myBitArray并不能改变原先的mybyte。
可以这么改变那一位为1,
mybyte[0] = (byte)(0x0020 | mybyte[0]);
-
byte[] mybyte = new byte[3] { 1, 2, 3 }; Console.WriteLine("1.byte[0]:{0}", mybyte[0]); BitArray myBitArray = new BitArray(mybyte); myBitArray.Set(3, myBitArray.Get(0)); myBitArray.CopyTo(mybyte, 0); Console.WriteLine("2.byte[0]:{0}", mybyte[0]); Console.ReadLine();
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已标记为答案 Demi_China 2013年3月18日 13:41