VB.Net - 逻辑/位运算符

2021-01-23 11:03 更新

下表显示了VB.Net支持的所有逻辑运算符。 假设变量A保持布尔值True,变量B保持布尔值False,则:

运算符描述Example
AndIt is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions.
它是逻辑以及按位AND运算符。 如果两个操作数都为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。
(A And B) is False.
OrIt is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions.
它是逻辑以及按位或运算符。 如果两个操作数中的任何一个为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。
(A Or B) is True.
NotIt is the logical as well as bitwise NOT operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false.
它是逻辑以及按位非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将为假。
Not(A And B) is True.
XorIt is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise, it returns False. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator
它是逻辑以及按位逻辑异或运算符。 如果两个表达式都为True或两个表达式都为False,则返回True; 否则,它返回False。 该运算符不会执行短路,它总是评估这两个表达式,并且没有该运算符的短路对应。
A Xor B is True.
AndAlsoIt is the logical AND operator. It works only on Boolean data. It performs short-circuiting.
它是逻辑AND运算符。 它只适用于布尔数据。 它执行短路。
(A AndAlso B) is False.
OrElseIt is the logical OR operator. It works only on Boolean data. It performs short-circuiting.
它是逻辑或运算符。 它只适用于布尔数据。 它执行短路。
(A OrElse B) is True.
IsFalseIt determines whether an expression is False.
它确定表达式是否为False。
 
IsTrueIt determines whether an expression is True.
它确定表达式是否为True。

尝试以下示例来了解VB.Net中提供的所有逻辑/按位运算符:

Module logicalOp

    Sub Main()
        Dim a As Boolean = True
        Dim b As Boolean = True
        Dim c As Integer = 5
        Dim d As Integer = 20
        'logical And, Or and Xor Checking
        If (a And b) Then
            Console.WriteLine("Line 1 - Condition is true")
        End If
        If (a Or b) Then
            Console.WriteLine("Line 2 - Condition is true")
        End If
        If (a Xor b) Then
            Console.WriteLine("Line 3 - Condition is true")
        End If
        'bitwise And, Or and Xor Checking
        If (c And d) Then
            Console.WriteLine("Line 4 - Condition is true")
        End If
        If (c Or d) Then
            Console.WriteLine("Line 5 - Condition is true")
        End If
        If (c Or d) Then
            Console.WriteLine("Line 6 - Condition is true")
        End If
        'Only logical operators
        If (a AndAlso b) Then
            Console.WriteLine("Line 7 - Condition is true")
        End If
        If (a OrElse b) Then
            Console.WriteLine("Line 8 - Condition is true")
        End If

        ' lets change the value of  a and b 
        a = False
        b = True
        If (a And b) Then
            Console.WriteLine("Line 9 - Condition is true")
        Else
            Console.WriteLine("Line 9 - Condition is not true")
        End If
        If (Not (a And b)) Then
            Console.WriteLine("Line 10 - Condition is true")
        End If
        Console.ReadLine()
    End Sub
End Module

当上述代码被编译和执行时,它产生以下结果:

Line 1 - Condition is true
Line 2 - Condition is true
Line 4 - Condition is true
Line 5 - Condition is true
Line 6 - Condition is true
Line 7 - Condition is true
Line 8 - Condition is true
Line 9 - Condition is not true
Line 10 - Condition is true

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部