VBScript bitwise operator

From StealthBot Wiki Backup
Revision as of 02:07, 1 February 2010 by Ribose (talk) (Binary)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

VBScript bitwise operators allow you to compare a number "per bit" in a similar manner to logical operators. Bitwise operators take in two (or one) integer (or long) values and return another integer (or long) value.

Binary

In order to understand these operators, you need to know that every number stored in computer memory is stored in "binary" format: 1s and 0s. Binary is the base-2 number system (as compared to the common base-10 number system) that allows us to describe the value of a number in the memory of a computer more accurately. A "bit" is each 1 or 0 digit in a binary number. A "byte" is 8 of these bits. A VBScript integer takes up 2 bytes (16-bit number) and a long takes up 4 bytes (32-bit number).

A binary number may be represented in text with a suffix of b. Here we will be representing in in a format similar to:

0000 0000b

Where bit 1 (1s place or "least significant" bit) is on the right before the b, 2s place next to it, 4s place next, and so on (in a similar manner that you would say 1s place, 10s place, 100s place for decimal).

VBScript has no way of taking in a binary number as a literal. Instead, you may use hexadecimal, a base-16 number system.

Bitwise operators do the exact same thing as the logical operators they share their name with, except that they do so on each of the bits of a number and the resulting integer is the combination of the resulting bits of the operation.

Bitwise conjunction operator: And

The bitwise And operator results in 1 only if both operand bits contain a 1. In any other case, And will return 0.

    1100b (12)
AND 1010b (10)
--------
    1000b = 8

Bitwise And is most often used to get a certain "part" of the binary number, notably in binary flags such as Battle.net user flags. If the user has multiple flags, a normal = comparison will fail to check for one of those. We can use the And operator to check for single flags (which you'll see correspond to a bit), such as checking for ops flag (2s place).

If BotFlags And 2 Then
    AddQ "This bot has ops."
End If

Lets say the bot has 0 flags.

    0000b
AND 0010b
---------
    0000b = 0

The result is 0, so the expression converts to False.

Lets say the bot has both ops (2s place bit), and UDP plug (16s place bit):

    1 0010b
AND 0 0010b
-----------
    0 0010b = 2

The result is 2, so the expression converts to True.

Bitwise disjunction operator: Or

The bitwise Or operator results in 1 when either of the two operand bits are 1. Only when both are 0 will Or return 0.

Or is most often used to do binary addition, where the duplicate bits are ignored:

   1100b (12)
OR 1010b (10)
--------
   1110b = 14

This can be used to "combine" binary flags, where addition would give different results if there were flag duplicates in the operands.

CombinedFlags = 2 Or UserX.Flags

The combined flag value in CombinedFlags will always have the 2s place set, plus whatever bits the user X's flags contain.

Bitwise negation operator: Not

The bitwise Not operator results in the opposite bitwise value of the single operand. Unlike other operators, Not only takes one operator to its right (it is a "unary" operator).

NOT 0000 0000 0001 0010b (34)
------------------------
    1111 1111 1110 1101b = -35

Here it negates the entire length of the integer (16 bits or 2 bytes).

It's worth noting that what happened here is the last "bit" stands for positive or negative (the reason why the range for integer or long types is 2^15 or 2^31 to -2^15 or -2^31).

Bitwise exclusion operator: Xor

The bitwise Xor operator (exclusive or) results in 1 only when one of the two operand bits are 1. When both are 1 or both are 0, the result is 0.

    1100b (12)
XOR 1010b (10)
---------
    0110b = 6

Bitwise equivalence operator: Eqv

The bitwise Eqv operator results in 1 bits when both operand bits are 1 or both operand bits are 0. If one operand bit is 1 and the other is 0, Eqv returns 0.

    1100b (12)
EQV 1010b (10)
---------
    1001b = 9

Bitwise implication operator: Imp

The bitwise Imp operator results in 1 in all cases when the second operand is 1 and when both operands are 0. The bit results in 0 only when the first operand is 1 and the second is 0.

    1100b (12)
IMP 1010b (10)
---------
    1011b = 11

See also