本文介绍了一个转移BitArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前竟将一个BitArray同时保持其长度。由于没有内置的方法,我努力建立一个,但不能让它工作,不幸的是。

I'm currently trying to shift a BitArray while keeping its length. Since there's no built-in method I'm struggling to build one but can't make it work, unfortunatly.

我最初的BitArray代码设置一个421长度。BitArray

My initial BitArray code sets a length of 421 for the BitArray.

var b = new BitArray(length: 421);



比,我指定用于测试一些值。例如:
b.Set(0,真);
b.Set(1,TRUE);

Than, I'm assigning some values for testing. For instance: b.Set(0, true); b.Set(1, true);

不过,我无法弄清楚如何位阵列移位。
尝试:
- 我认为我可以把它转换成长,不是让位操作。然而,长期不符合我的确切长度BitArray,这会导致错误以后当我申请两个BitArrays位运算(我的全部要求(数组1 | =数组2 >> 20)
- 我试着转换。 BitArray为byte [],执行操作,并返回它(参见的):

However, I can't figure out how to shift the bit array.Attempts: - I thought that I could convert it into long and than make the bit manipulation. However, long does not match my exact BitArray length, which results in errors later on when I apply bitwise operations on two BitArrays (my full requirements is (array1 |= array2 >> 20). - I tried to convert the BitArray into byte[], do the manipulation and return it (see Bit shifting N bits):

    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

However, byte's length is 8, which can't fit well with my length too. The result is 416 or 424 (another byte) instead of 421.


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

  • Finally, I tried the "primitive" way:

    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(如),但没有为我工作。

I also check up SO (e.g. BitArray - Shift bits) but nothing worked for me.

任何帮助将非常感激!

Any help will be very appreciated!

推荐答案

不过不是100%肯定有什么问题。这里有一个天真的实现:

Still not 100% sure what's the issue. Here's a naive implementation:

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 是在原始输入完成,而不是在第二的结果。

Note how bits are copied in the loops, and that the third PrintBitArray is done on the original input, not on the outcome of the second.

这篇关于一个转移BitArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:03