课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - BitArray

当前位置:免费教程 » 程序设计 » VB.Net
BitArray类管理位值的压缩数组,它表示为布尔值,其中true表示该位为(1),false表示位为off(0)。

它用于需要存储位但不提前知道位数。 您可以通过使用从零开始的整数索引来访问BitArray集合中的项目。

BitArray类的属性和方法

下表列出了BitArray类的一些常用属性:

属性描述
CountGets the number of elements contained in the BitArray.
获取BitArray中包含的元素数。
IsReadOnlyGets a value indicating whether the BitArray is read-only.
获取一个指示BitArray是否为只读的值。
ItemGets or sets the value of the bit at a specific position in the BitArray.
获取或设置位在BitArray中特定位置的值。
LengthGets or sets the number of elements in the BitArray.
获取或设置BitArray中的元素数。


下表列出了BitArray类的一些常用方法:
S.N方法名称和用途
1

Public Function And (value As BitArray) As BitArray

Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

对当前BitArray中的元素与指定的BitArray中的相应元素执行按位AND运算。

2

Public Function Get (index As Integer) As Boolean

Gets the value of the bit at a specific position in the BitArray.

获取位在BitArray中特定位置的值。

3

Public Function Not As BitArray

Inverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true.

反转当前BitArray中的所有位值,以便将设置为true的元素更改为false,将设置为false的元素更改为true。

4

Public Function Or (value As BitArray) As BitArray

Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

对当前BitArray中的元素与指定的BitArray中的相应元素执行按位或运算。

5

Public Sub Set (index As Integer, value As Boolean )

Sets the bit at a specific position in the BitArray to the specified value.

将BitArray中特定位置的位设置为指定值。

6

Public Sub SetAll (value As Boolean)

Sets all bits in the BitArray to the specified value.

将BitArray中的所有位设置为指定的值。

7

Public Function Xor (value As BitArray) As BitArray

Performs the bitwise eXclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

对当前BitArray中的元素与指定的BitArray中的相应元素执行逐位异或操作。


示例:

下面的例子演示了使用BitArray类:
  1. Module collections
  2. Sub Main()
  3. 'creating two bit arrays of size 8
  4. Dim ba1 As BitArray = New BitArray(8)
  5. Dim ba2 As BitArray = New BitArray(8)
  6. Dim a() As Byte = {60}
  7. Dim b() As Byte = {13}
  8. 'storing the values 60, and 13 into the bit arrays
  9. ba1 = New BitArray(a)
  10. ba2 = New BitArray(b)
  11. 'content of ba1
  12. Console.WriteLine("Bit array ba1: 60")
  13. Dim i As Integer
  14. For i = 0 To ba1.Count
  15. Console.Write("{0 } ", ba1(i))
  16. Next i
  17. Console.WriteLine()
  18. 'content of ba2
  19. Console.WriteLine("Bit array ba2: 13")
  20. For i = 0 To ba2.Count
  21. Console.Write("{0 } ", ba2(i))
  22. Next i
  23. Console.WriteLine()
  24. Dim ba3 As BitArray = New BitArray(8)
  25. ba3 = ba1.And(ba2)
  26. 'content of ba3
  27. Console.WriteLine("Bit array ba3 after AND operation: 12")
  28. For i = 0 To ba3.Count
  29. Console.Write("{0 } ", ba3(i))
  30. Next i
  31. Console.WriteLine()
  32. ba3 = ba1.Or(ba2)
  33. 'content of ba3
  34. Console.WriteLine("Bit array ba3 after OR operation: 61")
  35. For i = 0 To ba3.Count
  36. Console.Write("{0 } ", ba3(i))
  37. Next i
  38. Console.WriteLine()
  39. Console.ReadKey()
  40. End Sub
  41. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. Bit array ba1: 60
  2. False False True True True True False False
  3. Bit array ba2: 13
  4. True False True True False False False False
  5. Bit array ba3 after AND operation: 12
  6. False False True True False False False False
  7. Bit array ba3 after OR operation: 61
  8. True False True True False False False False

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号