|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.|
Arithmetic Operators in Visual Basic
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. Also classified with arithmetic operators are the bit-shift operators, which act at the level of the individual bits of the operands and shift their bit patterns to the left or right.
Negation also uses the - Operator (Visual Basic), but with only one operand, as the following example demonstrates.
Exponentiation uses the ^ Operator (Visual Basic), as the following example demonstrates.
Integer division is carried out using the \ Operator (Visual Basic). Integer division returns the quotient, that is, the integer that represents the number of times the divisor can divide into the dividend without consideration of any remainder. Both the divisor and the dividend must be integral types (SByte, Byte, Short, UShort, Integer, UInteger, Long, and ULong) for this operator. All other types must be converted to an integral type first. The following example demonstrates integer division.
Modulus arithmetic is performed using the Mod Operator (Visual Basic). This operator returns the remainder after dividing the divisor 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 also floating-point. The following example demonstrates this behavior.
Attempted Division by Zero
Division by zero has different results depending on the data types involved. In integral divisions (SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong), the .NET Framework throws a DivideByZeroException exception. In division operations on the Decimal or Single data type, the .NET Framework also throws a DivideByZeroException exception.
In floating-point divisions involving the Double data type, no exception is thrown, and 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 a Double value by zero.
Dividend data type
Divisor data type
NaN (not a mathematically defined number)
When you catch a DivideByZeroException exception, you can use its members to help you handle it. For example, the Message property holds the message text for the exception. For more information, see Structured Exception Handling Overview for Visual Basic.
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. You can shift the pattern to the right with the >> Operator (Visual Basic) or to the left with the << Operator (Visual Basic).
The data type of the pattern operand must be SByte, Byte, Short, UShort, Integer, UInteger, Long, or ULong. 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:
0 for an arithmetic left shift
0 for an arithmetic right shift of a positive number
0 for an arithmetic right shift of an unsigned data type (Byte, UShort, UInteger, ULong)
1 for an arithmetic right shift of a negative number (SByte, Short, Integer, or Long)
The following example shifts an Integer value both left and right.
Arithmetic shifts never generate overflow exceptions.
In addition to being logical operators, Not, Or, And, and Xor also perform bitwise arithmetic when used on numeric values. For more information, see "Bitwise Operations" in Logical and Bitwise Operators in Visual Basic.
Operands should normally be of the same type. For example, if you are doing addition with an Integer variable, you should add it to another Integer variable, and you should assign the result to a variable of type Integer as well.
One way to ensure good type-safe coding practice is to use the Option Strict Statement. If you set Option Strict On, Visual Basic automatically performs type-safe conversions. For example, if you try to add an Integer variable to a Double variable and assign the value to a Double variable, the operation proceeds normally, because an Integer value can be converted to Double without loss of data. Type-unsafe conversions, on the other hand, cause a compiler error with Option Strict On. For example, 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 you set Option Strict Off, however, Visual Basic allows implicit narrowing conversions to take place, although they can result in the unexpected loss of data or precision. For this reason, we recommend that you use Option Strict On when writing production code. For more information, see Widening and Narrowing Conversions.