0% found this document useful (0 votes)
41 views

Operators

The document discusses various operators in VB.Net, including arithmetic, comparison, logical/bitwise, and bit shift operators. It provides examples of common operators like addition, subtraction, logical AND, OR, comparison operators like equal and not equal. The document also explains how bitwise operators work on bits and perform operations, and bit shift operators can left shift or right shift binary values.

Uploaded by

nilesh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Operators

The document discusses various operators in VB.Net, including arithmetic, comparison, logical/bitwise, and bit shift operators. It provides examples of common operators like addition, subtraction, logical AND, OR, comparison operators like equal and not equal. The document also explains how bitwise operators work on bits and perform operations, and bit shift operators can left shift or right shift binary values.

Uploaded by

nilesh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

6/16/23, 4:10 AM VB.

Net - Operators

VB.Net - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. VB.Net is rich in built-in operators and provides following types of commonly
used operators −

Arithmetic Operators

Comparison Operators

Logical/Bitwise Operators

Bit Shift Operators

Assignment Operators

Miscellaneous Operators

This tutorial will explain the most commonly used operators.

Arithmetic Operators
Following table shows all the arithmetic operators supported by VB.Net. Assume variable A
holds 2 and variable B holds 7, then −

Show Examples

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 1/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Description Example

^ Raises one operand to the power of another B^A will give 49

+ Adds two operands A + B will give 9

- Subtracts second operand from the first A - B will give -5

* Multiplies both operands A * B will give 14

/ Divides one operand by another and returns a B / A will give 3.5


floating point result

\ Divides one operand by another and returns an B \ A will give 3


integer result

MOD Modulus Operator and remainder of after an integer B MOD A will give 1
division

Comparison Operators
Following table shows all the comparison operators supported by VB.Net. Assume variable A
holds 10 and variable B holds 20, then −

Show Examples

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 2/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Description Example

= Checks if the values of two operands are equal or (A = B) is not true.


not; if yes, then condition becomes true.

<> Checks if the values of two operands are equal or (A <> B) is true.
not; if values are not equal, then condition becomes
true.

> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand; if yes, then condition becomes
true.

< Checks if the value of left operand is less than the (A < B) is true.
value of right operand; if yes, then condition becomes
true.

>= Checks if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand; if yes, then
condition becomes true.

<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand; if yes, then
condition becomes true.

Apart from the above, VB.Net provides three more comparison operators, which we will be
using in forthcoming chapters; however, we give a brief description here.

Is Operator − It compares two object reference variables and determines if two object
references refer to the same object without performing value comparisons. If object1 and
object2 both refer to the exact same object instance, result is True; otherwise, result is
False.

IsNot Operator − It also compares two object reference variables and determines if two
object references refer to different objects. If object1 and object2 both refer to the exact
same object instance, result is False; otherwise, result is True.

Like Operator − It compares a string against a pattern.

Logical/Bitwise Operators
Following table shows all the logical operators supported by VB.Net. Assume variable A holds
Boolean value True and variable B holds Boolean value False, then −

Show Examples

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 3/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Description Example

And It is the logical as well as bitwise AND operator. If (A And B) is False.


both the operands are true, then condition becomes
true. This operator does not perform short-circuiting,
i.e., it evaluates both the expressions.

Or It is the logical as well as bitwise OR operator. If any (A Or B) is True.


of the two operands is true, then condition becomes
true. This operator does not perform short-circuiting,
i.e., it evaluates both the expressions.

Not It is the logical as well as bitwise NOT operator. Use Not(A And B) is True.
to reverses the logical state of its operand. If a
condition is true, then Logical NOT operator will
make false.

Xor It is the logical as well as bitwise Logical Exclusive A Xor B is True.


OR operator. It returns True if both expressions are
True or both expressions are False; otherwise it
returns False. This operator does not perform short-
circuiting, it always evaluates both expressions and
there is no short-circuiting counterpart of this
operator.

AndAlso It is the logical AND operator. It works only on (A AndAlso B) is False.


Boolean data. It performs short-circuiting.

OrElse It is the logical OR operator. It works only on Boolean (A OrElse B) is True.


data. It performs short-circuiting.

IsFalse It determines whether an expression is False.

IsTrue It determines whether an expression is True.

Bit Shift Operators


We have already discussed the bitwise operators. The bit shift operators perform the shift
operations on binary values. Before coming into the bit shift operators, let us understand the bit
operations.

Bitwise operators work on bits and perform bit-by-bit operations. The truth tables for &, |, and ^
are as follows −

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 4/9
6/16/23, 4:10 AM VB.Net - Operators

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows −

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

We have seen that the Bitwise operators supported by VB.Net are And, Or, Xor and Not. The
Bit shift operators are >> and << for left shift and right shift, respectively.

Assume that the variable A holds 60 and variable B holds 13, then −

Show Examples

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 5/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Description Example

And Bitwise AND Operator copies a bit to the result if it (A AND B) will give 12, which
exists in both operands. is 0000 1100

Or Binary OR Operator copies a bit if it exists in either (A Or B) will give 61, which is
operand. 0011 1101

Xor Binary XOR Operator copies the bit if it is set in one (A Xor B) will give 49, which is
operand but not both. 0011 0001

Not Binary Ones Complement Operator is unary and has (Not A ) will give -61, which is
the effect of 'flipping' bits. 1100 0011 in 2's complement
form due to a signed binary
number.

<< Binary Left Shift Operator. The left operands value is A << 2 will give 240, which is
moved left by the number of bits specified by the 1111 0000
right operand.

>> Binary Right Shift Operator. The left operands value A >> 2 will give 15, which is
is moved right by the number of bits specified by the 0000 1111
right operand.

Assignment Operators
There are following assignment operators supported by VB.Net −

Show Examples

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 6/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Description Example

= Simple assignment operator, Assigns values from C = A + B will assign value of A


right side operands to left side operand + B into C

+= Add AND assignment operator, It adds right operand C += A is equivalent to C = C +


to the left operand and assigns the result to left A
operand

-= Subtract AND assignment operator, It subtracts right C -= A is equivalent to C = C -


operand from the left operand and assigns the result A
to left operand

*= Multiply AND assignment operator, It multiplies right C *= A is equivalent to C = C *


operand with the left operand and assigns the result A
to left operand

/= Divide AND assignment operator, It divides left C /= A is equivalent to C = C / A


operand with the right operand and assigns the
result to left operand (floating point division)

\= Divide AND assignment operator, It divides left C \= A is equivalent to C = C \A


operand with the right operand and assigns the
result to left operand (Integer division)

^= Exponentiation and assignment operator. It raises the C^=A is equivalent to C = C ^ A


left operand to the power of the right operand and
assigns the result to left operand.

<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2

&= Concatenates a String expression to a String variable


Str1 &= Str2 is same as
or property and assigns the result to the variable or
property. Str1 = Str1 & Str2

Miscellaneous Operators
There are few other important operators supported by VB.Net.

Show Examples

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 7/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Description Example

AddressOf Returns the address of a


procedure. AddHandler Button1.Click,
AddressOf Button1_Click

Await It is applied to an operand in an


asynchronous method or
lambda expression to suspend Dim result As res
execution of the method until = Await AsyncMethodThatReturnsResult()
the awaited task completes. Await AsyncMethod()

GetType It returns a Type object for the


specified type. The Type object MsgBox(GetType(Integer).ToString())
provides information about the
type such as its properties,
methods, and events.

Function It declares the parameters and


Expression code that define a function Dim add5 = Function(num As
lambda expression. Integer) num + 5
'prints 10
Console.WriteLine(add5(5))

If It uses short-circuit evaluation


to conditionally return one of Dim num = 5
two values. The If operator can Console.WriteLine(If(num >= 0,
be called with three arguments "Positive", "Negative"))
or with two arguments.

Operators Precedence in VB.Net


Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator −

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.

Show Examples
https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 8/9
6/16/23, 4:10 AM VB.Net - Operators

Operator Precedence

Await Highest

Exponentiation (^)

Unary identity and negation (+, -)

Multiplication and floating-point division (*, /)

Integer division (\)

Modulus arithmetic (Mod)

Addition and subtraction (+, -)

Arithmetic bit shift (<<, >>)

All comparison operators (=, <>, <, <=, >, >=, Is, IsNot,
Like, TypeOf...Is)

Negation (Not)

Conjunction (And, AndAlso)

Inclusive disjunction (Or, OrElse)

Exclusive disjunction (Xor) Lowest

https://www.tutorialspoint.com/vb.net/vb.net_operators.htm 9/9

You might also like