Bitarray classes represent bit sets in close format. Although we can store a bit set in a regular array, we can create more effective programs if we use a Data Structure specially designed for the bit set. The bitarray class is used to process bit sets. A bit set can be used to effectively represent a set of Boolean values. Bitarray and arraylist are very similar. This is because bitarray can be dynamically adjusted in large and small ways, and binary bitarray is added as needed without worrying about array out-of-bounds issues.
1. Use the bitarray class. By instantiating a bitarray object, you can create a bitarray and transmit the number of binary bits in the array to the constructor. As follows:
Bitarray bitset = new bitarray (32 );
All 32 bits of this bitarray are set to false (false ). If you want them to be true, you can instantiate the array as follows:
Bitarray bitset = new bitarray (32, true );
The constructor can be overloaded in many different ways, but only one constructor method is described here. Bitarray can be instantiated using an array of byte (byte) values. For example:
Byte [] byteset = new byte [] {1, 2, 3, 4, 5 };
Bitarray bitset = new bitarray (byteset );
Bitset bitarray now contains binary bits whose values are 1, 2, 3, 4, and 5. Binary bits are stored in bitarray, and the highest valid bits (index 0) are at the leftmost position. When we read binary data from the right to the left, this may confuse people. For example, the content of the following eight-bit bitarray is equivalent to the value 1:
True False false
Of course, the most common method is to place the highest valid bit of the binary number on the right side, as shown below:
0 0 0 0 0 0 1
Therefore, you need to write code to change the display and binary sequence of binary values (instead of Boolean values. If bitarray contains byte (byte) values, each byte (Word
Section) each bit of the value is displayed. The following simple program segment cyclically traverses the bitarray of byte (byte) values:
byte[] ByteSet = new byte[] {1, 2, 3, 4, 5}; BitArray BitSet = new BitArray(ByteSet); for (int bits = 0; bits <= BbitSet.Count-1; bits++) Console.Write(BitSet.Get(bits) + " ");
The get method can be used to retrieve the separated bits stored in bitarray. This method removes an integer parameter, that is, the index of the value to be retrieved. Then, the value returned by this method is a bit that is expressed as true or false.
To display bitset bitarray values, the get method is used in the previous code segment. If the data stored in bitarray is indeed a binary value (in other words, the value should be displayed as 0 and 1 ), A method is required to display the actual values 1 and 0 in the correct order, where the correct order refers to starting from the right rather than starting from the left. Although the internal code used by the bitarray class cannot be changed, we can write external code to obtain the desired output.
The following program creates a bitarray with 5 bytes (bytes) values (namely, 1, 2, 3, 4, and 5, and each byte is displayed in the correct binary format:
using System;using System.Collections;class chapter6{ static void Main() { int bits; string[] binNumber = new string[8]; int binary; byte[] ByteSet = new byte[] { 1, 2, 3, 4, 5 }; BitArray BitSet = new BitArray(ByteSet); bits = 0; binary = 7; for (int i = 0; i <= BitSet.Count - 1; i++) { if (BitSet.Get(i) == true) binNumber[binary] = "1"; else binNumber[binary] = "0"; bits++; binary--; if ((bits % 8) == 0) { binary = 7; bits = 0; for (int ji = 0; ji <= 7; ji++) Console.Write(binNumber[ji]); Console.WriteLine(); } } }}
Bitarray is used to store a set of bits. Although bits are usually expressed as 0 and 1, The bitarray class stores these values with values true (1) or false (0. Bitarray is useful when you need to store a set of Boolean values, but it is even more useful when you need to process bits, this is because it is easy for people to move forward and backward between values of the Boolean (Boolean) type.