首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动BitArray

移动BitArray
EN

Stack Overflow用户
提问于 2014-08-31 22:14:40
回答 2查看 2.2K关注 0票数 0

我目前正在尝试移动一个BitArray,同时保持它的长度。由于没有内置的方法,我很难建立一个,但无法使它发挥作用,不幸。

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

代码语言:javascript
复制
var b = new BitArray(length: 421);

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

但是,我不知道如何转换位数组。尝试:-我认为我可以把它转换成长,而不是做位操作。但是,long与我的BitArray长度不匹配,这会在以后对两个BitArrays应用按位操作时产生错误(我的全部要求是(array1 |= array2 >> 20) )。-我尝试将BitArray转换为byte[],执行操作并返回它(参见Bit shifting N bits):

代码语言:javascript
复制
    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 < count)
        {
            // If the bit is set add its value to the byte
            if (array[bitpos])
                value += significance;

            bitpos++;

            if (bitpos % ByteLength == 0)
            {
                // A full byte has been processed, store it
                // increase output buffer index and reset work values
                bytes[bytepos] = value;
                bytepos++;
                value = 0;
                significance = 1;
            }
            else
            {
                // Another bit processed, next has doubled value
                significance *= 2;
            }
        }

        return bytes;
    }

    public static BitArray ShiftLeft(this BitArray array, int bitcount)
    {
        byte[] value = array.ToBytesArray();
        byte[] temp = new byte[value.Length];
        if (bitcount >= 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 < temp.Length - 1)
                {
                    temp[i] |= (byte)(temp[i + 1] >> 8 - bitcount % 8);
                }
            }
        }

        return new BitArray(temp);
    }

然而,字节的长度是8,这也不能与我的长度相匹配。结果是416或424 (另一个字节)而不是421。

  • 最后,我尝试了“原始”方式: (int i= 0;i<位数;i++) { var lastValue =数组;for (var j= 0;j< array.Length - 1;j++) { arrayj = arrayj + 1;} arrayarray.Length -1=lastValue};

我也是这样检查的(例如BitArray - Shift bits),但对我没有任何作用。

任何帮助都将非常感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-31 23:44:36

但仍然不能百分之百确定问题出在哪里。下面是一个简单的实现:

代码语言:javascript
复制
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是在原始输入上完成的,而不是在第二个输入的结果上完成的。

票数 0
EN

Stack Overflow用户

发布于 2015-11-27 04:12:43

代码语言:javascript
复制
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;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25596796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档