Section C.5. Logical and Bitwise Operators

   

C.5 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 both expression1 and expression2 evaluate to True . If either expression is False , then the result is False . If either expression is Null , then the result is Null . Its syntax is:

   result   =   expression1   And   expression2   

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, And returns 1 if the compared bits in both expressions are 1, and returns 0 in all other cases, as shown in the following table:

Bit in expression1

Bit in expression2

Result

1

1

1

1

1

For example, the result of 15 And 179 is 3, as the following binary representation shows:

 00000011 = 00001111 And 10110011 
AndAlso

As a comparison operator, works exactly like the And operator, except that it performs short-circuiting; an If statement will be evaluated from left to right only until the truth or falsity of the statement can be determined (that is, until the first False condition is encountered ). Unlike And , AndAlso does not double as a bitwise operator.

Or

Performs logical disjunction; that is, it returns True if and only if at least one (that is, one or both) of expression1 or expression2 evaluates to True . If either expression is Null , then the result is also Null . The syntax for the Or operator is:

   result   =   expression1   Or   expression2   

For example:

 If x = 5 Or y < 7 Then 

In this case, the code after the If statement will be executed if the value of x is five or if the value of y is less than seven.

As a bitwise operator, Or is the converse of And . Or returns 0 if the compared bits in both expressions are 0, and returns 1 in all other cases, as shown in the following table:

Bit in expression1

Bit in expression2

Result

1

1

1

1

1

1

1

For example, the result of 15 Or 179 is 191, as the following binary representation shows:

 10111111 = 00001111 Or 10110011 

And/Or: Conditional Short-Circuiting

The documentation implies that And and Or do no short-circuiting; that is, that every subexpression is evaluated, even if the result of the expression is known. In fact, both And and Or perform short-circuiting if the result of the expression is known and unevaluated subexpressions do not include calls to functions.

OrElse

As a comparison operator, works exactly like the Or operator, except that it performs short-circuiting; an If statement will be evaluated from left to right only until the truth or falsity of the statement can be determined (that is, until the first True condition is encountered). Unlike Or , OrElse does not double as a bitwise operator.

Not

Performs logical negation on a single expression; that is, it returns True if and only if the expression is False . If the expression is Null , though, the result of using the Not operator is still a Null . Its syntax is:

 result = Not   expression1   

For example:

 If Not IsNumeric(x) Then 

In this example, the code following the If statement will be executed if IsNumeric returns False , indicating that x is not a value capable of being represented by a number.

As a bitwise operator, Not simply reverses the value of the bit, as shown in the following table:

expression1

Result

1

1

For example, the result of Not 16 is 239, as the following binary representation shows:

 Not 00010000 = 11101111 
Xor

Performs logical exclusion; that is, Xor (an abbreviation for eXclusive OR) returns True if and only if the two expressions have different truth values. If either expression is Null , the result is also Null . Its syntax is:

   result = expression1   Xor   expression2   

As a bitwise operator, Xor returns 1 if the bits being compared are different and returns 0 if they are the same, as shown in the following table:

Bit in expression1

Bit in expression2

Result

1

1

1

1

1

1

Eqv and Imp

Eqv and Imp , two logical and bitwise operators, present in VB 6, have been removed from VB.NET. Eqv can simply be replaced with the = comparison operator. Hence, the expression:

 exp1 Eqv exp2 

is the same as:

 exp1 = exp2 

Imp can be replaced with an expression using the Not and Or operators. For example:

 exp1 Imp exp2 

can also be expressed as:

 (Not exp1) Or exp2 

For example, the result of 15 Xor 179 is 188, as the following binary representation shows:

 10111100 = 00001111 Imp 10110011 
   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net