我正在按照说明进行练习:
编写一个程序,对两个数字执行逻辑AND操作。
并伴随着这个输入和输出的例子:
AND
我不明白这两个数字的Sub Test()
Dim objResult As Object
Dim arrAllNumbers()
Dim arrAllAnimals()
Dim lngNumber As Long
Dim arrAnimals()
Dim strAnimals As String
' processing the table
Set objResult = ExtractOccurrences(Range("A2:B7"))
' example how to get array of the numbers
arrAllNumbers = objResult.Keys
' example how to get array of the dictionaries containing corresponding animals
arrAllAnimals = objResult.Items
' example how to get all animals for certain number
lngNumber = 2
arrAnimals = objResult(lngNumber).Keys
' convert to the string representation
strAnimals = "{""" & Join(arrAnimals, """,""") & """}"
End Sub
Function ExtractOccurrences(rngTable As Range) As Object
Dim arrTable() As Variant
Dim objList As Object
arrTable = rngTable
Set objList = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(arrTable, 1)
If IsEmpty(objList(arrTable(i, 2))) Then Set objList(arrTable(i, 2)) = CreateObject("Scripting.Dictionary")
objList(arrTable(i, 2))(arrTable(i, 1)) = ""
Next
Set ExtractOccurrences = objList
End Function
比较如何返回4的输出。我一直认为输出不能是0以外的任何数字,因为这两个数字不相同。
答案 0 :(得分:1)
问题写得不好;它们的意思是按位和(&
)。
>>> print(12 & 6)
4
要了解原因,您必须查看二进制值:
1100 (12)
0110 ( 6)
& ----
0100 ( 4)