O'Reilly logo

Visual Basic® 2010 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 16. Operators

An operator is a basic code element that performs some operation on one or more values to create a result. The values the operator acts upon are called operands. For example, in the following statement, the operator is + (addition), the operands are B and C, and the result is assigned to the variable A:

A = B + C

The Visual Basic operators fall into five categories: arithmetic, concatenation, comparison, logical, and bitwise. This chapter first explains these categories and the operators they contain, and then discusses other operator issues such as precedence, assignment operators, and operator overloading. Also included are discussions of some specialized issues that arise when you work with strings and dates.

ARITHMETIC OPERATORS

The following table lists the arithmetic operators provided by Visual Basic. Most programmers should be very familiar with most of them. The four operators that may need a little extra explanation are \, Mod, <<, and >>.

OPERATOR

PURPOSE

EXAMPLE

RESULT

^

Exponentiation

2 ^ 3

(2 to the power 3) = 2 * 2 * 2 = 8

-

Negation

−2

−2

*

Multiplication

2 * 3

6

/

Division

3 / 2

1.5

\

Integer division

17 \ 5

3

Mod

Modulus

17 Mod 5

2

+

Addition

2 + 3

5

2

Subtraction

3 - 2

1

<<

Bit left shift

10110111 >> 1

01101110

>>

Bit right shift

10110111 >> 1

01011011

The \ operator performs integer division. It returns the result of dividing its first operand by the second, dropping any remainder. It's important to understand that the result is truncated toward zero, not rounded. For example, 7 \ 4 = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required