Bit Arrays C# Help

If you need to deal with a number of bits, you can use the class :Array can the struct  BitVector32. BitArray is located in the namespace System.  BitVector32 is 4th the namespace System. Collect ions. Specialized. The difference between these two types is that Bit Array is resizable, which is useful 4,9/large number ,number of bits needed in advance, and it can contain a large number of bits. BitVector32 is stack-based and therefore faster. BitVector32 contains only 32 bits, which are stored in an integer.

Bit Array

The class BitArray is a reference type that contains an array of ints, where for every 32 bits a new integer is used. Members of this class are explained in the following table:


The helper method DisplayBits () iterates through a BitArray and displays 1 or 0 to the console, depending on whether or not the bit is set:

static void DisplayBits(BitArray bits)
foreach (bool bit in bits)
Console.Write(bit.? 1 . 0);



The example to demonstrate the BitArray class creates a bit array with 8 bits, indexed from 0 to 7. The SetAll () method sets all 8 bits to true. Then the Set () method changes bit 1 to false. Instead of the Set method, you can also use an indexer, as shown with index 5 and 7:

BitArray bits1 = new BitArray(8);
bits1.SetAll(true) ;
bits1.Set(1. false);
bits1[5] = false;
bits1[7] = false;
Console.Write(“initialized: “);
DisplayBits(bits1) ;
Console.WriteLine() ;

This is the displayed result of the initialized bits:

initialized: 10111010

The Not () method generates the inverse of the bits of the BitArray:

Console .write ( ” not “);
DisplayBits(bitsl) ;
bits1.Not ();
Console.Write(” = “);
DisplayBits(bits1) ;
Console.writeLine() ;

The result of Not () is all bits inversed. If the t it was true, it is false, and if it was false, it is true:

not 10111010 = 01000101

Here, a new BitArray is created. With the constructor, the variable bits is used to initialize the array, so the new array has the same values. Then the values for bits 0, 1, and 4 are set to different values. Before the Or () method is used, the bit array sorts  and bits 2 are displayed. The Or () method changes the values of bits 1.

BitArray bits2 = new BitArray(bits1);
bits2 [0] true;
bits2[1] = false;
bits2(4) = true;
OisplayBits(bits1) ;
Console.Write(· or .);
Console.Write(· = “);

With the Or () method, the set bits are taken from both input arrays. In the result, the bit is set if it was set with either the first or the second array:

01000101 or 10001101 = 11001101

Next, the And () method is used to operate on bits2 and bits1:

DisplayBits(bits2) ;
Console.Write(‘ and ‘);
Console.Write(‘ = ‘);

The result of the And () method only sets the bits where the bit was set in both input arrays:

10001101 and 11001101 = 10001101,

Finally the Xor () method is used for an exclusive OR:

Console.Write(‘ xor ‘);
Console.Write(‘ = ‘);

With the Xor () method, the resultant bits are set only if the bit was set either in the first or the second input, but not both:

11001101 xor 10001101 = 01000000


If you know the number of bits you need in advance, you can use the BitVector32 structure instead of BitArray. BitVect.r32 is more efficient, because it is a value type and stores the bits on the stack inside an integer. With a single integer you have a place for 32 bits. If you need more bits, you can use multiple BitVector32 values or the BitArray. The BitArray can grow as needed; this is not an option with BitVector32.

The next table shows the members of BitVector that are very differrent from BitArray.

The sample code creates a BitVector3 2 with the default constructor, where all 32 bits are initialized to false. Then masks are created to access the bits inside the bit vector. The first call to CreateMask () creates a mask to access the first bit. After CreateMask () is invoked, bi tl has a value of 1. Invoking CreateMask () once more and passing the first mask as a parameter to CreateMask () returns a mask to access the second bit, which is 2. bi t3 then has a value of 4 to access bit number 3. bi t4 has a value of 8 to access bit number 4.

Then the masks are used with the indexer to access the bits inside the bit vector and set fields accordingly:


The BitVector32 has an overridden ToString () method that not only displays the name of the class but aIso 1 or 0. if the bits are set or not, respectively.


Instead of creating a mask with the Crea teMask () method, you can define the mask yourself you can also set multiple bits at once. The hexadecimal value abcdef is the same as the binary value 1010 1011 1100 1101 1110 1111. All the bits defined with this value are set:

bits1[Oxabcdef] = true;

With the output shown you can view the bits that are set:


The bits are shown on the console as initialized:

BitVector32 (O101010110101l1l001l01l1l01l1ll)

Then six sections are created. The first section requires 12 bits, as defined by the hexadecimal value Oxfff (12 bits are set); section B requires 8 bits; section C, 4 bits; section D and E, 3 bits; and section F, 2 bits. The first call to CreateSection () just receives Oxfff to allocate the first 12 bits. With the second call to CreateSection (), the first section is passed as an argument, so that the next section continues where the first section ended. CreateSection () returns a value of type BitVector32. Section that contains the offset and the mask for the section.

BitVector32.Section sectionA =
BitVector32.Section sectionB =
sectionJI) ;
BitVector32.Section sectionC =
sectionB) ;
BitVector32.Section sectionD =
sectionC) ;
BitVector32.Section sectionE =
sectionD) ;
BitVector32.Section sectionF =
sectionE) ;

Passing a BitVector32. Section to the indexer of the BitVector32 returns an int just mapped: to the section of the bit vector. Here, a helper method, Int ToBinaryString (), retrieves a string representation of the int number:



The result displays the bit representation of sections A to F, which you can now verify with the value that was passed into the bit vector:

 Section A: 110111101111
Section B: 10111100
Section c: 1010
Section 0: 1
Section E: 111
Section F: 1

Posted on October 29, 2015 in Collections

Share the Story

Back to Top