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

image

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 = 1 and -7 \ 4 = -1 rather than 2 and -2 as you might expect.

The Mod operator returns the remainder after dividing its first operand by its second. For example, 17 Mod 5 = 2 because 17 = 3 * 5 + 2.

The << operator shifts the bits of an Integer value to the left, padding the empty bits on the right with zeros. For example, the byte value with bits 10110111 shifted 1 bit to the left gives 01101110 and shifting 10110111 2 bits to the left gives 11011100.

The >> operator shifts the bits of a value to the right, padding the empty bits on the left with zeros. For example, the byte value with bits 10110111 shifted 1 bit to the right gives 01011011 and shifting 10110111 2 bits to the right gives 00101101.

Unfortunately, Visual Basic doesn’t work easily with bit values, so you cannot use a binary value such as 10110111 in your code. Instead, you must write this value as the hexadecimal value &HB7 or the decimal value ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.