C# 移位位数组

C# 移位位数组,c#,bit-shift,bitarray,C#,Bit Shift,Bitarray,我当前正在尝试移动位数组,同时保持其长度。由于没有内置的方法,我正在努力构建一个,但不幸的是,它无法工作 我的初始位数组代码将位数组的长度设置为421 var b = new BitArray(length: 421); 然后,我为测试分配了一些值。例如: b、 设置(0,真); b、 Set(1,true) 但是,我不知道如何移动位数组。 尝试: -我想我可以把它转换成long,然后进行位操作。但是,long与我的精确位数组长度不匹配,这会导致以后在对两个位数组应用逐位操作时出错(我的全部要

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

我的初始位数组代码将位数组的长度设置为421

var b = new BitArray(length: 421);
然后,我为测试分配了一些值。例如: b、 设置(0,真); b、 Set(1,true)

但是,我不知道如何移动位数组。 尝试: -我想我可以把它转换成long,然后进行位操作。但是,long与我的精确位数组长度不匹配,这会导致以后在对两个位数组应用逐位操作时出错(我的全部要求是(array1 |=array2>>20)。 -我尝试将位数组转换为字节[],执行操作并返回它(请参阅):

public static byte[]ToBytesArray(此位数组,int startIndex,int count)
{
//获取存储所有字节所需的字节大小
int bytesize=计数/字节长度;
//在另一个字节上留下的任何位都是必需的
如果(通过TeleLength>0计算百分比)
{
bytesize++;
}
//为了结果
字节[]字节=新字节[bytesize];
//必须初始化为正确值,所有零位字节的值为零
//最低有效位的place值为1,每个位置都是
//左边是该值的两倍
字节值=0;
字节重要性=1;
int bytepos=0;
int-bitpos=startIndex;
while(bitpos-startIndex=8)
{
数组.Copy(值,位计数/8,临时值,0,临时长度-(位计数/8));
}
其他的
{
数组复制(值、温度、温度长度);
}
如果(比特数%8!=0)
{
对于(int i=0;i
然而,字节的长度是8,这与我的长度也不匹配。结果是416或424(另一个字节),而不是421

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

    for(int i=0;i
我也检查过,所以(例如),但对我来说没有任何效果


任何帮助都将不胜感激

仍然不能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();
}
void Main()
{
//创建并初始化大小为7的位数组(您有421个)。
bool[]myBools=新bool[7]{true,false,false,true,true,false,true};
BitArray myBA1=新的BitArray(myBools);
PrintBitArray(myBA1);//1001101
PrintBitArray(ShiftRight(myBA1));//0100110
PrintBitArray(ShiftLeft(myBA1));//0011010
}
位数组ShiftRight(位数组源){
bool[]new_arr=新bool[(aSource.Count)];
for(int i=0;i

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

无法100%确定问题是什么,以及这些链接是如何解决的。您想要一个可以左右移动的数组(我假设在移动时加0)或循环数组(因此,如果您向右移动,并且边上有一个1,它将移动到开头?)您是否遇到算术(最左边的位符号填充)和逻辑(最左边的位零填充)位移动之间的混淆?根据MSDN,左操作数(有符号或无符号整数类型)的性质决定了在C#规范下发生的情况。请注意,C++规范将该处方作为实现依赖项。我想要一个非循环操作。我用C++实现了一个使用STD::Buet到.NET的实现。我不认为这是我的问题,因为标准原语类型,如int或unit,不能帮助我,因为我需要自定义位数组长度。我的目标是转换C++ STD::BITSET操作符>
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;
        }
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();
}
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;
        }