1.59. C# BitArray

发布时间 :2023-10-12 23:00:08 UTC      

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.

1.59.1. 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.

1.59.2. 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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.