C# BitArray


Release date:2023-09-06 Update date:2023-10-13 Editor:admin View counts:220

Label:

C# BitArray

BitArray class manages a compact array of bit values, represented by Boolean values, where true indicates that the bit is on (1) false indicates that the bit is off (0).

Use a point array when you need to store bits but do not know the number of bits in advance. You can access items from the dot array collection using aninteger index, starting from scratch.

Methods and properties of the BitArray class

The following table lists some common properties of BitArray class:

Attribute

Description

Count

Gets the number of elements contained in the BitArray.

IsReadOnly

Gets a value indicating whether the BitArray is read-only.

Item

Gets or sets the value of the bit at the specified position in the BitArray.

Length

Gets or sets the number of elements in the BitArray.

The following table lists some common methods of BitArray:

Serial number

Method name and description

1

Public BitArray and (BitArray value); performs bitwise and operations on elements in the current BitArray and corresponding elements in the specifiedBitArray.

2

Public bool Get (int index); gets the value of the bit at the specified position in the BitArray.

3

Public BitArray Not (); reverses the bit value in the current BitArray so that the element set to true becomes false and the element set to false becomes true.

4

Public BitArray or (BitArray value); performs bitwise or operations on elements in the current BitArray and corresponding elements in the specifiedBitArray.

5

Public void Set (int index, bool value); sets the bit of the specified position in the BitArray to the specified value.

6

Public void SetAll (bool value); sets all bits in BitArray to the specified value.

7

Public BitArray Xor (BitArray value); performs bitwise XOR operations on elements in the current BitArray and corresponding elements in the specifiedBitArray.

Example

The following example demonstrates the use of a BitArray:

Example

using System;
using System.Collections;
namespace CollectionsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create two point arrays of size 8
            BitArray ba1 = new BitArray(8);
            BitArray ba2 = new BitArray(8);
            byte[] a = { 60 };
            byte[] b = { 13 };

            // Store values 60 and 13 in the point array
            ba1 = new BitArray(a);
            ba2 = new BitArray(b);
            // Content of ba1
            Console.WriteLine("Bit array ba1: 60");
            for (int i = 0; i < ba1.Count; i++)
            {
                Console.Write("{0, -6} ", ba1[i]);
            }
            Console.WriteLine();

            // Content of ba2
            Console.WriteLine("Bit array ba2: 13");
            for (int i = 0; i < ba2.Count; i++)
            {
                Console.Write("{0, -6} ", ba2[i]);
            }
            Console.WriteLine();


            BitArray ba3 = new BitArray(8);
            ba3 = ba1.And(ba2);
            // Content of ba3
            Console.WriteLine("Bit array ba3 after AND operation: 12");
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0, -6} ", ba3[i]);
            }
            Console.WriteLine();
            ba3 = ba1.Or(ba2);
            // Content of ba3
            Console.WriteLine("Bit array ba3 after OR operation: 61");
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0, -6} ", ba3[i]);
            }
            Console.WriteLine();

            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Bit array ba1: 60
False False True True True True False False
Bit array ba2: 13
True False True True False False False False
Bit array ba3 after AND operation: 12
False False True True False False False False
Bit array ba3 after OR operation: 61
True False True True False False False False

Powered by TorCMS (https://github.com/bukun/TorCMS).