Logical and Bitwise Operators
Logical
operators
allow you to evaluate one or more expressions and return a Boolean
value (True
or False
). VB.NET
supports four logical operators: And
,
AndAlso
, Or
,
OrElse
, Not
, and
Xor
. These operators also double as bitwise
operators. A bitwise comparison examines the bit positions in both
expressions and sets or clears the corresponding bit in the result,
depending upon the operator used. The result of a bitwise operation
is a numeric value.
In performing logical operations, VB.NET, unlike VB 6, uses
conditional
short- circuiting. This means that, in compound logical
expressions, the individual expressions are evaluated only until the
expression’s overall value is known, unless one of
the individual expressions involves a call to another function or
subroutine. Short-circuiting can occur in logical
And
operations when the first operand evaluates to
False
, as well as in logical Or
operations when the first operand evaluates to
True
.
The six logical and bitwise operators are:
-
And
Performs logical conjunction; that is, it returns
True
if and only if bothexpression1
andexpression2
evaluate toTrue
. If either expression isFalse
, then the result isFalse
. If either expression isNull
, then the result isNull
. Its syntax is:result
=expression1
Andexpression2
For example:
If (x = 5) And (y < 7) Then
In this case, the code after the
If
statement will be executed only if the value of x is five and the value of y is less than seven.As a bitwise operator, ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.