Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between set and set all in c#

When I use setall in program :

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

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 ?

like image 953
user2922938 Avatar asked Dec 11 '13 06:12

user2922938


People also ask

What are the differences between set & MultiSet?

In Set duplicate values are not allowed to get stored. On other hand in case of MultiSet we can store duplicate values. In case of Set, one cannot change the value once it gets inserted however we can delete or insert it again. However in case of MultiSet also we cannot change the value once get inserted.

What is set in C?

In the C programming language, the character set refers to a set of all the valid characters that we can use in the source program for forming words, expressions, and numbers. The source character set contains all the characters that we want to use for the source program text.

Is duplicate data allowed in set in C++?

Properties of set in C++ The property of Uniqueness: Every element of a set in C++ must be unique, i.e., no duplicate values are allowed.

What is the difference between list and set in C#?

The List is a type of data structure to store the elements. Sets are also a type of data structure but to stores the unique elements. 2. A sequence of the elements is important.


1 Answers

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

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.

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
like image 167
MarcinJuraszek Avatar answered Sep 21 '22 15:09

MarcinJuraszek