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

Appendix C. Operators

The Visual Basic operators fall into five main categories: arithmetic, concatenation, comparison, logical, and bitwise. The following sections explain these categories and the operators they contain. The end of this appendix describes special Date and TimeSpan operators, as well as operator overloading.

ARITHMETIC OPERATORS

The following table lists the arithmetic operators provided by Visual Basic.

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

Subtraction

3 − 2

1

<<

Bit left shift

&H57 << 1

&HAE

>>

Bit right shift

&H57 >> 1

&H2B

The bit shift operators deserve a little extra discussion. These operators shift the binary representation of a number by a given number of bits either left or right. Unfortunately, Visual Basic doesn't understand binary so you must manually translate between binary and decimal, octal, or hexadecimal.

For example, the hexadecimal value &H57 is 01010111 in binary. If you shift this one bit to the left, you get 10101110, which is &HAE in hexadecimal. If you shift the original value one bit to the right, you get 00101011, which is &H2B in hexadecimal.

When working with binary values, many developers prefer to work in hexadecimal because each hexadecimal digit corresponds to four binary bits so you can work with each group of four bits separately.

CONCATENATION OPERATORS

Visual Basic provides two concatenation ...

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