Section 5.3. Logical and Bitwise Operators


5.3. Logical and Bitwise Operators

Logical operators evaluate one or more expressions and return a Boolean result (true or False). VB supports six logical operators , many of which can also be used as bitwise operators, along with two bitwise-only operators. Bitwise operations work on integral (numeric integer) operands at the bit level and return numeric results. Other languages, such as C#, include distinct logical and bitwise operators , but for historical reasons, VB mostly uses a common set of operators for both types of operations.

If any of the operands are numeric (that is, non-Boolean), a bitwise operation is done instead of a logical operation. In cases where one operand is Boolean and the other is not, the Boolean operand is converted to a number first, using 0 for False and -1 for TRue.

In performing some logical operations, the .NET versions of Visual Basic use conditional short-circuiting , where complex conditional expressions are only partially evaluated if the final result of the entire expression can be determined without full evaluation. Individual expressions within a larger compound expression 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 AndAlso operations when the first operand evaluates to False, as well as in logical OrElse operations when the first operand evaluates to TRue. When using the more common And and Or operators, no short-circuiting is done.

Boolean operations always use the two Boolean values of TRue and False. Although Visual Basic's Boolean data type is based on the underlying .NET System.Boolean data type, its use in Visual Basic differs from that of other .NET languages. For historical reasons, Visual Basic's true value, when converted to a number, equates to -1. Other .NET languagesspecifically C#use a value of 1 for TRue. Although .NET resolves this difference through the shared data type, it can become an issue if you use a non-.NET data transfer method (such as a plain text file) to share numeric Boolean data between .NET languages.


And

The And operator performs a logical or bitwise conjunction on the two source operands. In logical operations, it returns true if and only if both operands evaluate to TRue. If either operand is False, then the result is False. The syntax is:

     result = expression1 And expression2 

For example, consider the following statement:

     If (x = 5) And (y < 7) Then 

In this case, the code within the Then clause will be executed only if the value of x is 5 and the value of y is less than 7.

As a bitwise operator, And returns 1 in a bit position if the compared bits in the same position in both expressions are 1, and it returns 0 in all other cases, as shown in the following table:

Bit in expression1

Bit in expression2

Result

0

0

0

0

1

0

1

0

0

1

1

1


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

     00001111 And 10110011    00000011 


AndAlso

The AndAlso operator works exactly like the logical And operator, but short-circuiting is enabled. If the first operand evaluates to False, the second operand is not evaluated at all, even if that expression includes function calls. Operands are evaluated from left to right. AndAlso does not perform bitwise operations.


Or

The Or operator performs a logical or bitwise disjunction on the two source operands. In logical operations, it returns true if either of the operands evaluates to TRue. If both operands are False, then the result is False. The syntax is:

     result = expression1 Or expression2 

For example, consider the following statement:

     If (x = 5) Or (y < 7) Then 

In this case, the code within the Then clause will be executed if either the value of x is 5 or the value of y is less than 7.

As a bitwise operator, Or returns 1 in a bit position if either of the compared bits in the same position in the source expressions are 1, and it returns 0 in all other cases, as shown in the following table:

Bit in expression1

Bit in expression2

Result

0

0

0

0

1

1

1

0

1

1

1

1


For example, the bitwise result of 15Or179 is 191, as the following binary representation shows:

     00001111 Or 10110011    10111111 


OrElse

The OrElse operator works exactly like the logical Or operator, but short-circuiting is enabled. If the first operand evaluates to TRue, the second operand is not evaluated at all, even if that expression includes function calls. Operands are evaluated from left to right. OrElse does not perform bitwise operations.


Not

The Not operator performs a logical or bitwise negation on a single expression. In logical operations, it returns TRue if the operand is False, and False if the operand is true. The syntax is:

     result = Not expression1 

For example, consider the following statement:

     If Not IsNumeric(x) Then 

In this example, the code within the Then clause 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 toggles the value of each bit in the source expression between 0 and 1, as shown in the following table:

Bit in expression1

Result

0

1

1

0


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

     Not 00010000    11101111 


Xor

The Xor (an abbreviation for "eXclusive OR") operator performs a logical or bitwise exclusion on the two source operands. In logical operations, it returns true if and only if the two expressions have different truth values. If both expressions are TRue, or both are False, this operator returns False. If one of the operands is true but the other False, then Xor returns true. The syntax is:

     result = expression1 Xor expression2 

As a bitwise operator, Xor returns 1 in a bit position if the compared bits are different from each other, and it returns 0 if they are the same, as shown in the following table:

Bit in expression1

Bit in expression2

Result

0

0

0

0

1

1

1

0

1

1

1

0


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

     00001111 Xor 10110011    10111100 

Eqv and Imp

Eqv and Imp, two logical and bitwise operators present in VB 6, have been removed from .NET implementations of Visual Basic. Eqv can be replaced with the = (equal to) comparison operator. The expression:

     expression1 Eqv expression2 

is the same as the logical comparison:

     expression1 = expression2 

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

     expression1 Imp expression2 

can also be expressed as:

     (Not expression1) Or expression2 

If you need more precise replacements using bitwise calculations, see the "Logical and Bitwise Operators" section in Appendix D.



<< (Shift Left)

New in 2003. The << (shift left) operator performs a left shift of the bits in the first operand by the number of bits specified in the second operand. All bits shifted off the left are lost. All bits newly vacated on the right are filled with zeros.

The number of bits you can shift is limited by the number of possible bits in the first operand. Any excess number of shift positions will be ignored. This operator never throws an overflow exception. The syntax is:

     result = source << bits 

For example, the bitwise result of 15 << 5 is 224, as the following binary representation shows:

     00001111 << 5    11100000 


>> (Shift Right)

New in 2003. The >> (shift right) operator performs a right shift of the bits in the first operand by the number of bits specified in the second operand. All bits shifted off the right are lost. All bits newly vacated on the left are filled with the bit value of the leftmost bit position before shifting. When shifting unsigned data values (Byte, UShort, UInteger, ULong), the newly vacated bits on the left are filled with zero (0).

The number of bits you can shift is limited by the number of possible bits in the first operand. Any excess number of shift positions will be ignored. This operator never throws an overflow exception. The syntax is:

     result = source >> bits 

For example, the bitwise result of 12 >> 1 is 6, as the following binary representation shows:

     00001100 >> 1    00000110 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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