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
Trueif and only if bothexpression1andexpression2evaluate toTrue. If either expression isFalse, then the result isFalse. If either expression isNull, then the result isNull. Its syntax is:result=expression1Andexpression2For example:
If (x = 5) And (y < 7) Then
In this case, the code after the
Ifstatement will be executed only if the value of x is five and the value of y is less than seven.As a bitwise operator, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access