共用方式為


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.

Arithmetic Operations

You can add two values in an expression together with the + Operator (Visual Basic), or subtract one from another with the - Operator (Visual Basic), as the following example demonstrates.

Dim x As Integer
x = 67 + 34
x = 32 - 12

Negation also uses the - Operator (Visual Basic), but with only one operand, as the following example demonstrates.

Dim x As Integer = 65
Dim y As Integer
y = -x

Multiplication and division use the * Operator (Visual Basic) and / Operator (Visual Basic), respectively, as the following example demonstrates.

Dim y As Double
y = 45 * 55.23
y = 32 / 23

Exponentiation uses the ^ Operator (Visual Basic), as the following example demonstrates.

Dim z As Double
z = 23 ^ 3
' The preceding statement sets z to 12167 (the cube of 23).

Integer division is carried out using the \ Operator. 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.

Dim k As Integer
k = 23 \ 5
' The preceding statement sets k to 4.

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.

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.

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 Dividend value Result

Double

Double

0

NaN (not a mathematically defined number)

Double

Double

> 0

PositiveInfinity

Double

Double

< 0

NegativeInfinity

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.

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. 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.

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.

Bitwise Operations

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.

Type Safety

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.

See Also

Reference

Arithmetic Operators (Visual Basic)
Bit Shift Operators

Concepts

Comparison Operators in Visual Basic
Concatenation Operators in Visual Basic
Logical and Bitwise Operators in Visual Basic
Efficient Combination of Operators