VB.Net - Logical/Bitwise Operators


Advertisements

Following table shows all the logical operators supported by VB.Net. Assume variable A holds Boolean value True and variable B holds Boolean value False, then −

Operator Description Example
And It 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. (A And B) is False.
Or It 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.
Not It 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.
Xor It is the logical as well as bitwise Logical Exclusive OR operator. It returns False if both expressions are True or both expressions are False; otherwise, it returns True. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator A Xor B is True.
AndAlso It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. (A AndAlso B) is False.
OrElse It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. (A OrElse B) is True.
IsFalse It determines whether an expression is False.
IsTrue It determines whether an expression is True.

Try the following example to understand all the logical/bitwise operators available in 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

When the above code is compiled and executed, it produces the following result −

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - 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
vb.net_operators.htm
Advertisements