Arithmetic Operators in Visual Basic
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
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, 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 (
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. 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.
Dim x As Integer = 100 Dim y As Integer = 6 Dim z As Integer z = x Mod y ' The preceding statement sets z to 4.
Dim a As Double = 100.3 Dim b As Double = 4.13 Dim c As Double c = a Mod b ' The preceding statement sets c to 1.18.
Division by zero has different results depending on the data types involved. In integral divisions (
ULong), the .NET Framework throws a DivideByZeroException exception. In division operations on the
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||Dividend value||Result|
|0||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 Try...Catch...Finally Statement.
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 or to the left with the << Operator.
The data type of the pattern operand must be
ULong. The data type of the shift amount operand must be
Integer or must widen to
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 (
1 for an arithmetic right shift of a negative number (
The following example shifts an
Integer value both left and right.
Dim lResult, rResult As Integer Dim pattern As Integer = 12 ' The low-order bits of pattern are 0000 1100. lResult = pattern << 3 ' A left shift of 3 bits produces a value of 96. rResult = pattern >> 2 ' A right shift of 2 bits produces value of 3.
Arithmetic shifts never generate overflow exceptions.
In addition to being logical operators,
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
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.