|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Arithmetic operators are used to perform many of the familiar arithmetic operations that involve the calculation of numeric values represented by literals, variables, other expressions, function and property calls, and constants.
You can add two values in an expression together with the + operator, or subtract one from another with the - operator, as shown in the following examples:
Dim x As Integer x = 67 + 34 x = 32 - 12
Negation also uses the - operator, but with somewhat different syntax, as shown below:
Dim x As Integer = 65 Dim y As Integer y = -x
Multiplication and division use the * and / operators, respectively, as in these examples:
Dim y As Double y = 45 * 55.23 y = 32 / 23
Exponentiation uses the ^ operator, as shown below:
Dim z As Double z = 23 ^ 3 ' z equals the cube of 23.
Integer division is carried out using the \ operator. Integer division returns the number of times one integer can evenly divide into another. Only integral types (Byte, Short, Integer, and Long) can be used with this operator. All other types must be converted to an integral type first. The following is an example of integer division:
Dim k As Integer k = 23 \ 5 ' k = 4
Modulus arithmetic is performed using the Mod operator. This operator returns the remainder after the divisor is divided into the dividend an integral number of times. If both divisor and dividend are integral types, the returned value is integral. If divisor and dividend are floating-point types, the returned value is a floating point variable. Two examples illustrate this behavior:
Dim x As integer = 100 Dim y As Integer = 6 Dim z As Integer z = x Mod y ' z equals 4. Dim a As Double = 100.3 Dim b As Double = 4.13 Dim c As Double c = a Mod b ' c equals 1.18.
Division by Zero
Division by zero has different results depending on the data types involved. In integral divisions (Short, Integer, Long), a System.DivideByZeroException is thrown. In division operations on the Decimal or Single data type, Visual Basic also throws a System.DivideByZeroException exception. In floating-point divisions with the Double variable type, however, the result is the class member representing NaN, PositiveInfinity, or NegativeInfinity, depending on the dividend. The following table summarizes the various results of attempting to divide by zero.
|Dividend data type||Divisor data type||Dividend value||Result|
|Double||Double||0||System.Double.NaN (not a number)|
When you catch a System.DivideByZeroException exception, you can use its members to help you handle it. The HelpLink property has a link to the appropriate help file, the Message property holds the message text for the exception, and the StackTrace property contains the trace, which should indicate where the error occurred. The GetBaseException method returns the original exception that caused this DivideByZeroException, as well as any other exceptions linked to it.
Bitwise operations evaluating two values in binary (base 2) form, compare the bits at corresponding positions, and then assign values based on the comparison, as in the demonstration below of bitwise And:
Dim x As Integer x = 3 And 5 ' x = 1
In this example, the value of
- First, the values are converted to binary form, as shown below:
3 in binary form = 011
5 in binary form = 101
- Next, the binary representations are compared, one binary position at a time. If both bits at a given position are 1, then a 1 is placed in that position in the result. If either bit is 0, then a 0 is placed in that position in the result. In our example this works out as follows:
011 3 in binary form
101 5 in binary form
001 The result, in binary form
Converting back to decimal, 001 is the binary representation of 1, so x = 1.
The bitwise Or operation is similar, except that a 1 is assigned to the result bit if either of the compared bits is 1. Xor assigns a 1 to the result bit if only one of, but not both of, the compared bits is 1. Not takes a single operand and inverts all the bits, including the sign bit, and assigns that value to the result. This means that for positive numbers, Not always returns a negative. Likewise, for negative numbers, Not always returns a positive or zero.
Note Bitwise operations can be performed on integral types only. Floating point values must be converted to integral types before bitwise operation can proceed.
Bit Shift Operations
A bit shift operation performs an arithmetic shift on a bit pattern. The pattern is contained in the operand on the left, while the operand on the right specifies the number of positions to shift the pattern. The data type of the pattern operand must be Byte, Short, Integer, or Long. The data type of the shift amount operand must be Integer or must widen to Integer.
Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. The bit positions vacated by a shift are set as follows:
- Zero for an arithmetic left shift
- Zero for an arithmetic right shift of a positive number
- Zero for an arithmetic right shift of any pattern with data type Byte
- One for an arithmetic right shift of a negative number (Short, Integer, or Long)
In the following example, an Integer value is shifted both left and right:
Dim Pattern As Integer = 12 ' Low-order bits are 0000 1100 Dim LResult, RResult As Integer LResult = Pattern << 3 ' Left shift of 3 bits produces value of 96 RResult = Pattern >> 2 ' Right shift of 2 bits produces value of 3
Arithmetic shifts never generate overflow exceptions.
Operands should normally be of the same type. For example, if you are adding an Integer variable, you should add it to another Integer variable, and the variable that the value is assigned to should be of type Integer as well.
One way to ensure good type-safe coding practice is to use Option Strict. If Option Strict is On, Visual Basic .NET automatically performs type-safe conversions. Thus, if you try to add an Integer variable to a Double variable and assign the value to a Double variable, the operation proceeds normally. Unsafe, or narrowing conversions, on the other hand, cause a compiler error. Thus if you try to add an Integer variable to a Double variable and assign the value to an Integer variable, a compiler error results, because a Double variable cannot be implicitly converted to type Integer.
If Option Strict is Off, however, these implicit narrowing conversions are allowed to take place, but can result in the unexpected loss of data or precision. For this reason, it is recommended that you use Option Strict On when writing production code. For additional information about conversions, see Widening and Narrowing Conversions, and for information about Option Strict, see Option Strict Statement.