分享方式:


Visual Basic 中的邏輯運算子和位元運算子

邏輯運算子會比較 Boolean 運算式並傳回 Boolean 結果。 AndOrAndAlsoOrElseXor 運算子為二元,因為它們採用兩個運算元,而 Not 運算子則為一元,因為其只採用一個運算元。 其中有些運算子也可以對整數值執行位邏輯運算。

一元邏輯運算子

Not 運算子會對 Boolean 運算式執行邏輯否定。 其會產生其運算元的邏輯相反。 如果運算式評估為 True,則 Not 會傳回 False;如果運算式評估為 False,則 Not 會傳回 True。 說明如下例。

Dim x, y As Boolean
x = Not 23 > 14
y = Not 23 > 67
' The preceding statements set x to False and y to True.

二元邏輯運算子

And 運算子會對兩個 Boolean 運算式執行邏輯結合。 如果兩個運算式都評估為 True,則 And 會傳回 True。 如果其中至少一個運算式評估為 False,則 And 會傳回 False

Or 運算式會對兩個 Boolean 運算式執行分離包含。 如果任一運算式評估為 True,或兩個都評估為 True,則 Or 會傳回 True。 如果兩個運算式都不評估為 True,則 Or 會傳回 False

Xor 運算子會對兩個 Boolean 運算式執行邏輯排除。 如果剛好只有一個運算式評估為 True,則 Xor 會傳回 True。 如果兩個運算式都評估為 TrueFalse,則 Xor 會傳回 False

下列範例說明 AndOrXor 運算子。

Dim a, b, c, d, e, f, g As Boolean

a = 23 > 14 And 11 > 8
b = 14 > 23 And 11 > 8
' The preceding statements set a to True and b to False.

c = 23 > 14 Or 8 > 11
d = 23 > 67 Or 8 > 11
' The preceding statements set c to True and d to False.

e = 23 > 67 Xor 11 > 8
f = 23 > 14 Xor 11 > 8
g = 14 > 23 Xor 8 > 11
' The preceding statements set e to True, f to False, and g to False.

最少運算邏輯作業

AndAlso 運算子非常類似 And 運算子,因為其也會對兩個 Boolean 運算式執行邏輯結合。 這兩者之間的主要差異在於 AndAlso 會呈現最少運算行為。 如果 AndAlso 運算式中的第一個運算式評估為 False,則因為第二個運算式不會改變最終結果,所以不會評估第二個運算式,而 AndAlso 會傳回 False

同樣地,OrElse 運算子會對兩個 Boolean 運算式執行最少運算邏輯分離。 如果 OrElse 運算式中的第一個運算式評估為 True,則因為第二個運算式不會改變最終結果,所以不會評估第二個運算式,而 OrElse 會傳回 True

最少運算權衡取捨

最少運算可以藉由評估無法改變邏輯運算結果的運算式來改善效能。 不過,如果該運算式會執行其他動作,則最少運算會跳過這些動作。 舉例來說,如果某運算式包含對 Function 程序的呼叫,則當運算式屬於最少運算時並不會呼叫該程序,而 Function 中包含的任何其他程式碼都不會執行。 因此,函式可能只會偶爾執行,而且可能無法正確測試。 或者,程式邏輯可能會相依於 Function 中的程式碼。

下列範例說明 AndOr 和各自最少運算的差異。

Dim amount As Integer = 12
Dim highestAllowed As Integer = 45
Dim grandTotal As Integer
If amount > highestAllowed And checkIfValid(amount) Then
    ' The preceding statement calls checkIfValid().
End If
If amount > highestAllowed AndAlso checkIfValid(amount) Then
    ' The preceding statement does not call checkIfValid().
End If
If amount < highestAllowed Or checkIfValid(amount) Then
    ' The preceding statement calls checkIfValid().
End If
If amount < highestAllowed OrElse checkIfValid(amount) Then
    ' The preceding statement does not call checkIfValid().
End If
Function checkIfValid(ByVal checkValue As Integer) As Boolean
    If checkValue > 15 Then
        MsgBox(CStr(checkValue) & " is not a valid value.")
        ' The MsgBox warning is not displayed if the call to
        ' checkIfValid() is part of a short-circuited expression.
        Return False
    Else
        grandTotal += checkValue
        ' The grandTotal value is not updated if the call to
        ' checkIfValid() is part of a short-circuited expression.
        Return True
    End If
End Function

請注意,在上述範例中,當呼叫為最少運算時,checkIfValid() 中的某些重要程式碼不會執行。 即便 12 > 45 傳回 False,第一個 If 陳述式也會呼叫 checkIfValid(),因為 And 不屬於最少運算。 第二個 If 陳述式不會呼叫 checkIfValid(),因為當 12 > 45 傳回 False 時,AndAlso 會對第二個運算式進行最少運算。 即便 12 < 45 傳回 True,第三個 If 陳述式也會呼叫 checkIfValid(),因為 Or 不屬於最少運算。 第四個 If 陳述式不會呼叫 checkIfValid(),因為當 12 < 45 傳回 True 時,OrElse 會對第二個運算式進行最少運算。

位元運算

位元運算會以二元 (base 2) 格式評估兩個整數值。 它們會比較對應位置的位元,然後根據比較來指派值。 下列範例說明 And 運算子。

Dim x As Integer
x = 3 And 5

上述範例將 x 的值設定為 1。 這會因為以下原因發生:

  • 值會被視為二進位:

    二進位格式的 3 = 011

    二進位格式的 5 = 101

  • And 運算子會比較二進位表示法,一次一個二進位位置 (位元)。 如果位於指定位置的兩個位元都是 1,則 1 會放在結果中的該位置。 如果任一位元為 0,則 0 會放在結果中的該位置。 在上述範例中,其運作方式如下:

    011 (二進位格式的 3)

    101 (二進位格式的 5)

    001 (二進位格式的結果)

  • 結果會被視為十進位。 值 001 是 1 的二進位表示法,因此 x = 1。

位元 Or 運算很類似,但如果兩個比較的位元為 1,則會將 1 指派給結果位元。 如果剛好一個 (非兩個) 比較的位元為 1,則 Xor 會將 1 指派給結果位元。 Not 會採用單一運算元並反轉所有位元,包括正負號位元,並將該值指派給結果。 這表示對於帶正負號的正數,Not 一律會傳回負值,而負數 Not 則一律會傳回正或零值。

AndAlsoOrElse 運算子不支援位元運算。

注意

位元運算只能對整數型別執行。 浮點數必須轉換成整數型別,才能繼續位元運算。

另請參閱