返回到:VB.Net – 集合
BitArray类管理位值的压缩数组,它表示为布尔值,其中true表示该位为(1),false表示位为off(0)。
它用于需要存储位但不提前知道位数。 您可以通过使用从零开始的整数索引来访问BitArray集合中的项目。
BitArray类的属性和方法
下表列出了BitArray类的一些常用属性:
属性 | 描述 |
---|---|
Count | 获取BitArray中包含的元素数。 |
IsReadOnly | 获取一个指示BitArray是否为只读的值。 |
Item | 获取或设置位在BitArray中特定位置的值。 |
Length | 获取或设置BitArray中的元素数。 |
下表列出了BitArray类的一些常用方法:
S.N | 方法名称和用途 |
---|---|
1 | Public Function And (value As BitArray) As BitArray对当前BitArray中的元素与指定的BitArray中的相应元素执行按位AND运算。 |
2 | Public Function Get (index As Integer) As Boolean获取位在BitArray中特定位置的值。 |
3 | Public Function Not As BitArray反转当前BitArray中的所有位值,以便将设置为true的元素更改为false,将设置为false的元素更改为true。 |
4 | Public Function Or (value As BitArray) As BitArray对当前BitArray中的元素与指定的BitArray中的相应元素执行按位或运算。 |
5 | Public Sub Set (index As Integer, value As Boolean )将BitArray中特定位置的位设置为指定值。 |
6 | Public Sub SetAll (value As Boolean)将BitArray中的所有位设置为指定的值。 |
7 | Public Function Xor (value As BitArray) As BitArray对当前BitArray中的元素与指定的BitArray中的相应元素执行逐位异或操作。 |
示例:
下面的例子演示了使用BitArray类:
HTML
x
46
46
1
Imports System.Collections
2
Module collections
3
Sub Main()
4
'creating two bit arrays of size 8
5
Dim ba1 As BitArray = New BitArray(8)
6
Dim ba2 As BitArray = New BitArray(8)
7
Dim a() As Byte = {60}
8
Dim b() As Byte = {13}
9
'storing the values 60, and 13 into the bit arrays
10
ba1 = New BitArray(a)
11
ba2 = New BitArray(b)
12
'content of ba1
13
Console.WriteLine("Bit array ba1: 60")
14
Dim i As Integer
15
16
For i = 0 To ba1.Count - 1
17
Console.Write("{0 } ", ba1(i))
18
Next i
19
Console.WriteLine()
20
'content of ba2
21
Console.WriteLine("Bit array ba2: 13")
22
23
For i = 0 To ba2.Count -1
24
Console.Write("{0 } ", ba2(i))
25
Next i
26
Console.WriteLine()
27
Dim ba3 As BitArray = New BitArray(8)
28
ba3 = ba1.And(ba2)
29
'content of ba3
30
Console.WriteLine("Bit array ba3 after AND operation: 12")
31
32
For i = 0 To ba3.Count -1
33
Console.Write("{0 } ", ba3(i))
34
Next i
35
Console.WriteLine()
36
ba3 = ba1.Or(ba2)
37
'content of ba3
38
Console.WriteLine("Bit array ba3 after OR operation: 61")
39
40
For i = 0 To ba3.Count -1
41
Console.Write("{0 } ", ba3(i))
42
Next i
43
Console.WriteLine()
44
Console.ReadKey()
45
End Sub
46
End Module
当上述代码被编译和执行时,它产生以下结果:
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
返回到:VB.Net – 集合
阅读剩余 68%
作者:terry,如若转载,请注明出处:https://www.web176.com/vbnet_api/11492.html