VBA 中的布尔逻辑
Boolean logic in VBA
我在使用两个 AND 运算符和数字时发现意外行为:第三个条目似乎被视为模 2。请参见下面的示例。
我很想知道为什么会这样。
Sub Test()
Dim a As Boolean
Dim b As Boolean
a = True And 1 And 2 ' a = false
b = True And 2 And 3 ' b = true
MsgBox (a)
MsgBox (b)
End Sub
And
在这里按位应用
1111 'true
And 0001 '1
And 0010 '2
-------------
0000 '0 -> evaluates to false when cast as boolean
1111 'true
And 0010 '2
And 0011 '3
-------------
0010 '2 -> evaluates to true when cast as boolean
我在使用两个 AND 运算符和数字时发现意外行为:第三个条目似乎被视为模 2。请参见下面的示例。
我很想知道为什么会这样。
Sub Test()
Dim a As Boolean
Dim b As Boolean
a = True And 1 And 2 ' a = false
b = True And 2 And 3 ' b = true
MsgBox (a)
MsgBox (b)
End Sub
And
在这里按位应用
1111 'true
And 0001 '1
And 0010 '2
-------------
0000 '0 -> evaluates to false when cast as boolean
1111 'true
And 0010 '2
And 0011 '3
-------------
0010 '2 -> evaluates to true when cast as boolean