转移BitArray

我正在尝试在保持其长度的同时移动BitArray。 由于没有内置的方法,我正在努力构建一个,但不能让它工作,不幸的是。

我的初始BitArray代码为BitArray设置了421的长度。

var b = new BitArray(length: 421); 

比,我正在为测试分配一些值。 例如:b.Set(0,true); b.Set(1,true);

但是,我无法弄清楚如何移位位arrays。 尝试: – 我认为我可以将它转换为长而不是进行位操作。 但是,long与我的确切BitArray长度不匹配,当我在两个BitArrays上应用按位运算时,会导致错误(我的完整要求是(array1 | = array2 >> 20)。 – 我试图将BitArray转换为byte [ ],进行操作并返回它(参见位移N位 ):

  public static byte[] ToBytesArray(this BitArray array, int startIndex, int count) { // Get the size of bytes needed to store all bytes int bytesize = count / ByteLength; // Any bit left over another byte is necessary if (count % ByteLength > 0) { bytesize++; } // For the result byte[] bytes = new byte[bytesize]; // Must init to good value, all zero bit byte has value zero // Lowest significant bit has a place value of 1, each position to // to the left doubles the value byte value = 0; byte significance = 1; int bytepos = 0; int bitpos = startIndex; while (bitpos - startIndex = 8) { Array.Copy(value, bitcount / 8, temp, 0, temp.Length - (bitcount / 8)); } else { Array.Copy(value, temp, temp.Length); } if (bitcount % 8 != 0) { for (int i = 0; i < temp.Length; i++) { temp[i] <<= bitcount % 8; if (i > 8 - bitcount % 8); } } } return new BitArray(temp); } 

但是,字节的长度是8,这也不适合我的长度。 结果是416或424(另一个字节)而不是421。

  • 最后,我尝试了“原始”的方式:

      for (int i = 0; i < bitcount; i++) { var lastValue = array[0]; for (var j = 0; j < array.Length - 1; j++) { array[j] = array[j + 1]; } array[array.Length - 1] = lastValue; } 

我也检查了SO(例如BitArray – Shift位 ),但没有任何对我有用 。

任何帮助将非常感谢!

仍然不是100%确定问题是什么。 这是一个天真的实现:

 void Main() { // Creates and initializes a BitArrays of size 7 (you have 421). bool[] myBools = new bool[7] { true,false,false,true,true,false,true }; BitArray myBA1 = new BitArray(myBools ); PrintBitArray(myBA1); // 1001101 PrintBitArray(ShiftRight(myBA1)); // 0100110 PrintBitArray(ShiftLeft (myBA1)); // 0011010 } BitArray ShiftRight(BitArray aSource) { bool[] new_arr = new bool[( aSource.Count)]; for (int i = 0; i < aSource.Count -1; i++) new_arr[i+1] = aSource[i]; return new BitArray(new_arr); } BitArray ShiftLeft(BitArray aSource) { bool[] new_arr = new bool[( aSource.Count)]; for (int i = 0; i < aSource.Count -1; i++) new_arr[i] = aSource[i+1]; return new BitArray(new_arr); } string PrintBitArray(BitArray aSource) { StringBuilder sb = new StringBuilder(); foreach (var bit in aSource) { sb.Append( (bool)bit ? 1 : 0 ); } return sb.ToString(); } 

注意如何在循环中复制位,并且第三个PrintBitArray是在原始输入上完成的,而不是在第二个的结果上完成的。

 public static bool[] Left_shiftBitArray(bool[] Array, int count) { Array = BitArray_LRotat(Array, count); for (int i=Array.GetLength(0)-1; i>=(Array.GetLength(0)-count); i--) { Array[i] = false; } return Array; } public static bool[] BitArray_LRotat(bool[] input, int x) { //bool [] temp= new bool[input.Length]; bool[] final = new bool[input.Length]; for (int i = input.Length; i > x; i--) { final[i - x - 1] = input[i - 1]; } for (int i = x; i > 0; i--) { final[(input.Length) - i] = input[x - i]; } return final; } 
Interesting Posts