c#中set和set all之间的差异

时间:2021-08-13 13:28:12

When I use setall in program :

当我在程序中使用setall时:

BitArray bb = new BitArray(8) ;
bb.SetAll( true);

int[] dd = new int[1];           

bb.CopyTo(dd, 0);

for (int i = 0; i < dd.Length; i++)
    Console.WriteLine(dd[i]);

// result is  -1

but if i use set for every element of bitarray

但如果我使用set为bitarray的每个元素

BitArray bb = new BitArray(8) ;
bb.Set( 0,true);
bb.Set(1, true);
bb.Set(2, true);
bb.Set(3, true);
bb.Set(4, true);
bb.Set(5, true);
bb.Set(6, true);
bb.Set(7, true);

int[] dd = new int[1];           

bb.CopyTo(dd, 0);         

for ( int i = 0; i < dd.Length; i++)
    Console.WriteLine(dd[i]);

// result is 255

Why different result in two program when use set result is -1 and when use setall in second program result is 255 ?

当使用设置结果为-1并且在第二个程序结果中使用setall为255时,为什么不同的结果导致两个程序?

1 个解决方案

#1


8  

That's because SetAll() method looks like that:

那是因为SetAll()方法看起来像这样:

public void SetAll(bool value)
{
    int num = value ? -1 : 0;
    int arrayLength = BitArray.GetArrayLength(this.m_length, 32);
    for (int i = 0; i < arrayLength; i++)
    {
        this.m_array[i] = num;
    }
    this._version++;
}

BitArray uses int[] internally to store your bits. To get new BitArray(8) it uses just one int, because that's enough to store 8 bits. But the entire allocated memory is used when you use CopyTo method to get int[], so you get all 32 bits from underlying int. and because when you use for loop you only set 8 least meaningful bits you get 255 when cast to int[] after using the loop and -1 when you do that using SetAll() method.

BitArray在内部使用int []来存储您的位。为了获得新的BitArray(8),它只使用一个int,因为这足以存储8位。但是当您使用CopyTo方法获取int []时,将使用整个分配的内存,因此您可以从底层int获取所有32位。并且因为当你使用for循环时,你只设置8个最有意义的位,你在使用循环后转换为int []时得到255,而当你使用SetAll()方法转换为-1时得到-1。

You can prove that.

你可以证明这一点。

for (int i = 1; i <= 32; i++)
{
    BitArray bb = new BitArray(i);
    bb.SetAll(true);

    BitArray bb2 = new BitArray(i);
    for (int j = 0; j < i; j++)
        bb2.Set(j, true);

    int[] dd = new int[1];
    int[] dd2 = new int[1];

    bb.CopyTo(dd, 0);
    bb2.CopyTo(dd2, 0);
    Console.WriteLine("{0} - {1}", dd[0], dd2[0]);
}

Code above prints:

以上代码打印:

-1 - 1
-1 - 3
-1 - 7
-1 - 15
-1 - 31
-1 - 63
-1 - 127
-1 - 255
-1 - 511
-1 - 1023
-1 - 2047
-1 - 4095
-1 - 8191
-1 - 16383
-1 - 32767
-1 - 65535
-1 - 131071
-1 - 262143
-1 - 524287
-1 - 1048575
-1 - 2097151
-1 - 4194303
-1 - 8388607
-1 - 16777215
-1 - 33554431
-1 - 67108863
-1 - 134217727
-1 - 268435455
-1 - 536870911
-1 - 1073741823
-1 - 2147483647
-1 - -1

#1


8  

That's because SetAll() method looks like that:

那是因为SetAll()方法看起来像这样:

public void SetAll(bool value)
{
    int num = value ? -1 : 0;
    int arrayLength = BitArray.GetArrayLength(this.m_length, 32);
    for (int i = 0; i < arrayLength; i++)
    {
        this.m_array[i] = num;
    }
    this._version++;
}

BitArray uses int[] internally to store your bits. To get new BitArray(8) it uses just one int, because that's enough to store 8 bits. But the entire allocated memory is used when you use CopyTo method to get int[], so you get all 32 bits from underlying int. and because when you use for loop you only set 8 least meaningful bits you get 255 when cast to int[] after using the loop and -1 when you do that using SetAll() method.

BitArray在内部使用int []来存储您的位。为了获得新的BitArray(8),它只使用一个int,因为这足以存储8位。但是当您使用CopyTo方法获取int []时,将使用整个分配的内存,因此您可以从底层int获取所有32位。并且因为当你使用for循环时,你只设置8个最有意义的位,你在使用循环后转换为int []时得到255,而当你使用SetAll()方法转换为-1时得到-1。

You can prove that.

你可以证明这一点。

for (int i = 1; i <= 32; i++)
{
    BitArray bb = new BitArray(i);
    bb.SetAll(true);

    BitArray bb2 = new BitArray(i);
    for (int j = 0; j < i; j++)
        bb2.Set(j, true);

    int[] dd = new int[1];
    int[] dd2 = new int[1];

    bb.CopyTo(dd, 0);
    bb2.CopyTo(dd2, 0);
    Console.WriteLine("{0} - {1}", dd[0], dd2[0]);
}

Code above prints:

以上代码打印:

-1 - 1
-1 - 3
-1 - 7
-1 - 15
-1 - 31
-1 - 63
-1 - 127
-1 - 255
-1 - 511
-1 - 1023
-1 - 2047
-1 - 4095
-1 - 8191
-1 - 16383
-1 - 32767
-1 - 65535
-1 - 131071
-1 - 262143
-1 - 524287
-1 - 1048575
-1 - 2097151
-1 - 4194303
-1 - 8388607
-1 - 16777215
-1 - 33554431
-1 - 67108863
-1 - 134217727
-1 - 268435455
-1 - 536870911
-1 - 1073741823
-1 - 2147483647
-1 - -1