1.59. C# BitArray

发布时间 : 2025-10-25 13:34:35 UTC      

Page Views: 9 views

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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。