Logical, Bitwise and Shift Operators

Visual Basic 2010 offers logical, bitwise, and shift operators. Logical operators are special operators enabling comparisons between Boolean values and also returning Boolean values. Bitwise and shift operators enable performing operations bit by bit. Next let’s discuss both logical and bitwise operators.

Logical Operators

In Visual Basic 2010, there are eight logical/bitwise operators: Not, And, Or, Xor, AndAlso, OrElse, IsFalse, IsTrue. In this subsection you learn about the first four, while the other ones will be covered in the next subsection. The first operator, Not, basically returns the opposite of the actual Boolean value. For example, the following lines of code return False because although the 43 number is greater than 10, Not returns the opposite:

'Returns False
Dim result As Boolean = (Not 43 > 10)

Logical operators can also be used with reference types. For example, you can return the opposite of the result of a comparison between objects (see section “Comparison Operators” for details):

image

This code returns True; the comparison between firstPerson and secondPerson returns False because they point to two different instances of the Person class, but Not returns the opposite. Generally you use such operators for reverting the state of an object basing on a Boolean property. The next operator is And, which compares two Boolean values or expressions and returns True if both values or expressions are True; otherwise, if at least one value is False, And returns False. Here is an example of And:

image

And is also useful for comparing Boolean properties of objects. For example, you might want to check if a text file exists on disk and that it is not zero-byte; you could write the following code:

image

If both actions return True, And returns True. In our example this should mean that we encountered a valid text file. The next operator is Or. Such an operator works like this: if expressions or values are True, it returns True; if both are False, it returns False; and if one of the two expressions is True, it returns True. The following code demonstrates this scenario:

image

The last operator is Xor (eXclusive Or). Such an operator compares two Boolean expressions (or values) and returns True only if one of the two expressions is True whereas in all other cases it returns False. Continuing the first example, Xor returns the values described inside comments:

image

Short-Circuiting Operators

There are situations in which you do not need to perform the evaluation of the second expression in a Boolean comparison, because evaluating the first one provides the result you need. In such scenarios you can use two short-circuiting operators, AndAlso and OrElse. Short-circuiting means that code execution is shorter and performances are improved. Such operators are particularly useful when you need to invoke an external method from within an If..Then code block. For example, let’s consider again the previous example for the And operator:

image

The Visual Basic compiler performs both evaluations. What would it happen if the file does not exists? It throws a FileNotFoundException when the ReadAllText method is invoked, because the And operator requires both expressions to be evaluated. Of course, you should implement error handling routines for such code, but this example is just related to operators. You can simply prevent your code from encountering the previously described problem using AndAlso. You need to replace And with AndAlso, as in the following code:

image

AndAlso evaluates the first expression; if this returns False, the second expression is not evaluated at all. In this case, if the file does not exist, the code exits from the If block. AndAlso’s counterpart is OrElse, which evaluates the second expression only when the first one is False. Finally, in Visual Basic there are two other operators named IsTrue and IsFalse. The first one works in conjunction with the OrElse operator while the second one with the AndAlso. You cannot explicitly invoke such operators in your code because it is the job of the Visual Basic compiler invoking them within an evaluation expression. This means that types that you want to be evaluated via OrElse or AndAlso must expose both of them. The following is a simple sample:

image

Bitwise Operators

Performing bitwise operations basically means performing operations with two binary numbers, bit by bit. The problem here is that Visual Basic does not allow working directly with binary numbers, so you need to write code against decimal or hexadecimal numbers that the Visual Basic compiler will actually treat, behind the scenes, in their binary representation, but you still need to write them in a comprehensible way.

Converting Between Decimal and Binary

You can use the Windows calculator to perform conversions between decimal/hexadecimal and binary numbers.

Bitwise operators in Visual Basic are still And, Or, Not, and Xor. But different from logical operations, in which such operators evaluate expressions, bitwise operations are related to bit manipulations. You might wonder why you would need to perform bitwise operations in the era of WPF, Silverlight, and other high-level technologies. You could get multiple answers to this question, but probably the most useful one is providing the example of applications that interact with hardware devices in which there is still the need of working in a bit-by-bit fashion. Another common situation in Visual Basic is the combination of Enum flags. Let’s now see some examples. The And operator combines two operands into a result; inside such a result, it puts a 1 value where both operands have 1 in a particular position; otherwise it puts a zero. For a better explanation, consider the following code:

Dim result As Integer = 152 And 312

The binary counterpart for 152 is 10011000, whereas the binary counterpart for 312 is 100111000. The result variable’s value is 24, whose binary counterpart is 11000. If you observe the following representation

image

you can notice how the third line, which represents the result of the And operation, contains 1 only in positions in which both operands have 1. If you then convert the result back to a decimal number, you will get 24. The Or operator works similarly: It combines two operands into a result; inside such a result, it puts a 1 value if at least one of the operands has a 1 value in a particular position. Consider this code:

Dim result As Integer = 152 Or 312

Both 152 and 312 binary counterparts are the same as the previous example. The Or operator produces 110111000 as a binary output, whose decimal counterpart is 440. To understand this step, take a look at this comparison:

image

It’s easy to see that the result contains 1 where at least one of the operands contains 1 in a particular position. The Xor operator combines two operands into a result; inside such a result, it puts a 1 value if at least one of the operands has a 1 value in a particular position, but not if both have 1 in that position. (In such a case it places 0.) Consider this bitwise operation:

Dim result As Integer = 152 Xor 312

The 152 and 312 binary counterparts are the same as in the preceding example. But this line of code returns 416, whose binary counterpart is 110100000. So let’s see what happened:

image

As you can see, Xor placed 1 where at least one of the operands has 1 in a particular position, but where both operands have 1, it placed 0. The Not operator is probably the easiest to understand. It just reverses the bits of an operand into a result value. For example, consider this line of code:

Dim result As Integer = Not 312

In the following comparison, the second line is the result of the preceding negation:

100111000
011000111

This result has −313 as its decimal counterpart.

Binary Numbers

This book does not teach binary numbers, so the code shown in this and in the following section assumes that you are already familiar with binary representations of decimal numbers.

Shift Operators

Shift operators are also something that makes more sense with binary numbers than with decimal or hexadecimal numbers, although you need to provide them via their decimal representations. Basically with shift operators you can move (that is, shift) a binary representation left or right for the specified number of positions. The left-shift operator is << whereas the right-shift operator is >>. For example, consider the following Integer:

image

The binary representation for 324 is 101000100. At this point we want to left-shift such binary for four positions. The following line accomplishes this:

image

With the left-shifting of four positions, the number 101000100 produces 1010001000000 as a result. Such binary representation is the equivalent of the 5184 decimal number, which is the actual value of the leftValue variable. The right-shift operator works the same but moves positions on the right:

image

This code moves 101000100 for four positions to the right, so the binary result is 10100. Its decimal equivalent is then 20, which is the actual value of the rightValue variable.

Supported Types

Shift operators support Byte, Short, Integer, Long, SByte, UShort, UInteger and ULong data types. When using shift operators with unsigned types, there is no sign bit to propagate and therefore the vacated positions are set to zero.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.143.65