\
Skip Navigation LinksHome > Coding > Scripting > VBScript > Operators\
 
 
[]UDF / UDC Library
[]COM Reference
[]Script Library



 

Jump To:

Arithmetic | Concatenation | Comparison | Bitwise Operators | Operator Precedence

Arithmetic

Arithmetic can be used against any numeric data type. The following symbols are used:

! Name Description | Example
+ Addition Used to add numeric values together.
dblResults = dblValueA + dblValueB
13+5=18


- Subtraction Used to subtract numeric values together or negate a value.
dblResults = dblValueA - dblValueB
13-5=8


/ Standard
Division
Used to divide numeric values.  This style of division returns a floating-point number (fractional number)
dblResults = dblValueA / dblValueB
13/5=2.6


\ Integer
Division
Used to divide integer values. This style of division returns a whole number (integer).  No remainder
Results = dblValueA \ dblValueB
13\5=2


MOD Modulo Used to perform division on two numeric values and return only the remainder
intResults = dblValueA MOD dblValueB
13 mod 5=3


* Multiplication Used to multiply numeric values together.
dblResults = dblValueA * dblValueB
13*5=65


^ Exponentiation Raises a number to the power of the exponent.
dblResults = dblValueA ^ dblValueB
13^5=371293

Concatenation

Concatenation is only used on string variables to combine them. It does not perform any math.

! Name Description | Example
& Standard Concantionates two strings together
strValue = strPart & strPart
"Good " & "Day" = "Good Day"


+ Non-Standard Concantionates two strings together
strValue = strPart & strPart
"Good " + "Day" = "Good Day"

Comparison

Comparison is used to compare the contents of two variables against each other.

! Name Description | Example
> Greater Than Expression1 is greater than and not equal to Expression2
bRetVal = dblX > dblY
13 > 5 = True
< Less Than Expression1 is less than and not equal to Expression2
bRetVal = dblX < dblY
13 < 5 = False
<> Not Equal Expression1 is not equal to Expression2. Can also be written as ><.
bRetVal = dblX <> dblY
13 <> 5 = True
>= Greater Than
Or Equal
Expression1 is greater than or equal to Expression2. Can also be written as =>.
bRetVal = dblX >= dblY
13 >= 5 = True
<= Less Than
Or Equal
Expression1 is less than or equal to Expression2. Can also be written as =<.
bRetVal = dblX <= dblY
13 <= 5 = False
= Equal Expression1 is equal to Expression2
bRetVal = dblX = dblY
13 = 5 = False
IS Is Object1 is Object2, or Object1 is a condition
bRetVal = obj1 IS obj2
bRetVal = obj1 IS [Condition]
objWSH IS objWSH = True
objWSH IS Nothing

Bitwise Operators

Bitwise operations are used to evaluate one or more expressions and return logical values.

AND | OR | XOR | NOT

AND - bitwise conjunction

As a comparison operator: Returns True only if Expression1 and Expression2 are true.

Comparison Examples:
boolReturn = Expression1 AND Expression2
(y = 15) AND (x < 23) = [Boolean]
Expression1 Expression2 Result
True
True
False
False
True
False
True
False
True
False
False
False

As a bitwise operator:  Compares the bits in both numeric expressions and returns a value based on the following:

Expression1 Bit Expression2 Bit Result Bit
0
0
1
1
0
1
0
1
0
0
0
1

Quick review of how binary values are read. Each number is a sequence of eight zeros or ones. A single zero or one is called a bit. A group of eight bits is a single byte. Within a byte, each bit is in a place-weighted position, assuming a decimal value as follows:

128| 64| 32| 16|  8|  4|  2|  1

The total value of the byte is determined by adding the value of the placeholder each bit occupies, where a bit value of 0 indicates that placeholder value is not included, and a value of 1 indicates that it is:

Byte:    10011010
Decimal: 128 + 0 + 0 + 16 + 8 + 0 + 2 + 0
or just: 128 + 16 + 8 + 2 = 154

So when using this bitwise operator, the numeric values are evaluated at the bit level using the above comparison chart to resolve each position within the byte:

(147) 10010011
( 58) 00111010
( 18) 00010010

So, with this example, 147 AND 58 = 18

OR - bitwise disjunction

As a comparison operator: Returns True if Expression1 or Expression2 are true, and if Expression1 and Expression2 are true.

Comparison Examples:
boolReturn = Expression1 OR Expression2
(y = 15) OR (x < 23) = [Boolean]
Expression1 Expression2 Result
True
True
False
False
True
False
True
False
True
True
True
False

As a bitwise operator:  Compares the bits in both numeric expressions and returns a value based on the following:

Expression1 Bit Expression2 Bit Result Bit
0
0
1
1
0
1
0
1
0
1
1
1

So when using a bitwise operator, the numeric values are evaluated at the bit level using the above comparison chart to resolve each position within the byte:

(147) 10010011
( 58) 00111010
(187) 10111011

So, with this example, 147 OR 58 = 187

XOR - bitwise exclusion

As a comparison operator: Returns True if Expression1 or Expression2 are true.

Comparison Examples:
boolReturn = Expression1 XOR Expression2
(y = 15) XOR (x < 23) = [Boolean]
Expression1 Expression2 Result
False
True
False
True
False
False
True
True
False
True
True
False

As a bitwise operator:  Compares the bits in both numeric expressions and returns a value based on the following:

Expression1 Bit Expression2 Bit Result Bit
0
1
0
1
0
0
1
1
0
1
1
0

Essentially, the numeric values are evaluated at a binary level:
(147) 10010011
(58)  00111010
(169) 10101001


So, with this example, 147 Xor 58 = 169

NOT - bitwise negation

As a comparison operator: Negates the Boolean value of Expression1

Expression1 NOT Result
True
False
NOT
NOT
False
True

As a bitwise operator:  Compares the bits in both numeric expressions and returns a value based on the following:

Expression1 Bit NOT Result Bit
0
1
NOT
NOT
1
0

Unfortunately, evaluation is not quite as straightforward as you might think.  If Not True is False, and Not bit 1 is bit 0, you would think that NOT byte 01010101 is byte 10101010 as this follows the same conversion as we've seen before - it isn't.  The key to understanding how to negate is to first discard the idea that NOT is the same thing as opposite.  To demonstrate this, look at the value outcome of a simple bitwise operation from a place-weighted perspective:

         128| 64| 32| 16|  8|  4|  2|  1
Value:                         1   1   0 = six
Negate:                        0   0   1 = one
or
Value:     0   0   0   0   0   1   1   0 = six
Negate:    1   1   1   1   1   0   0   1 = two hundred and fourth nine

Execute the following line in VBScript: WScript.Echo NOT 6 however, and you will get an answer of -7 . Similar if you execute the line WScript.Echo NOT False you will get an answer of -1. If you look back at our negation of 6, the straightforward reversal of the bits does not accurately return the negation of the number when compared against the truth of NOT 0 = -1. First thing to wonder is how do you represent a negative number in binary? There are a few accepted ways, but the one used for this method is called complementation.  For this application and overly simplified, this means that you evaluate the place-weighted position for the largest value, and then designate the next highest position as a "flag" position, where 0 indicates that the position is a positive number (but not included), and 1 a negative (and that position is included).  In our above example, the highest position that the value of six would take is the third from right.  As a positive number, we would then write the number as such:

         128| 64| 32| 16|  8|  4|  2|  1
Value:                     0   1   1   0 = six

Then using bit negation as defined within the table above, we have:

         128| 64| 32| 16|  8|  4|  2|  1
Value:                     0   1   1   0 = six        ( 0+4+2+0=  6)
Negate:                    1   0   0   1 = - seven    (-8+0+0+1= -7)

Getting further into inner workings of binary numbers is beyond the scope of this article, but there is an excellent explanation of negative binary numbers at All About Circuits

Operator Precedence

When evaluating a calculation line, the line is processed in the following operator order, and within each operator class, in the order listed.  When evaluating a line, multiple identical operators are processed from left to right.

# Category Precedence
1 Arithmetic ^, (/ *), \, MOD, (+ -)
2 Concatenation (& +)
3 Comparison No defined order, evaluated as listed from left to right
4 Logical NOT, AND, OR, XOR

If you need to override any given precedence, you place the single calculation element within parenthesis. 

As an example of how operator precedence works, copy/paste this code snippet into notepad, save it and run it to see how the totals differ:  Change the uncommented line to see the different calculations.

Dim dblRetVal

dblRetVal = 1 + 1 / 5
'dblRetVal = (1 + 1) / 5

WScript.Echo dblRetVal

 FAQs  |  Terms Of Use  |  Privacy Policy  |  Contact Us
Copyright © 1997 - 2010 Dx21, LLC. All rights reserved.
Dx21, LLC a Washington Limited Liability Company
Page Rendered at: 9/2/2010 8:16:37 PM for Unknown