Previous Section Table of Contents Next Section

Boolean Math

Boolean math is a special kind of logical math. If you know how to subnet TCP/IP addresses, you already know Boolean math, although you may not realize it. First, here are the basic Boolean operators that VBScript supports:

  • NOT: Reverses a value from 0 to 1 (or False to True) or vice versa.

  • AND: Returns a True if both values are True.

  • OR: Returns a True if either value is True.

  • XOR: Returns a True if one, but not both, values are True.

In VBScript, the value zero represents False; all other values represent True. Internally, VBScript generally uses -1 to represent True. To demonstrate Boolean math, try the following examples:


'Not

MsgBox NOT True

MsgBox NOT False



'And

MsgBox True AND False

MsgBox True AND True



'Or

MsgBox True OR False

MsgBox True OR True



'Xor

MsgBox True XOR False

MsgBox True XOR True

You should get the following results in message boxes:

  • False (the opposite of True)

  • True (the opposite of False)

  • False (both values aren't True)

  • True (both values are True)

  • True (at least one value is True)

  • True (at least one value is True)

  • True (only one value is True)

  • False (both values are True)

So who cares? You'll primarily deal with Boolean math like this in the form of setting flags. For example, Windows domains (Active Directory and NT) store a user flags value, which controls several things, like whether the user account is locked out, expired, disabled, and so forth. The flags are stored as a single byte of information, and each bit in the flag has a different meaning. For example:

  • Bit 1, with a value of 1, indicates if the account is locked out.

  • Bit 2, with a value of 2, indicates if the account has expired.

  • Bit 3, with a value of 4, indicates if the account is disabled.

  • Bit 4, with a value of 8, indicates if the password needs to be changed.

  • Bit 5, with a value of 16, indicates if the user can change his password.

All bytes have bits, and all bits have a value. In the number 5, for example, bits 1 and 3 are turned on. Their combined values (1 + 4) create the value of 5. To test to see if a value is on or not, you can use the AND operator:


'Assume variable vFlag has a flag byte in it

If vFlag AND 1 Then

 MsgBox "Account locked out"

End If



If vFlag AND 2 Then

 MsgBox "Account expired"

End If



If vFlag AND 4 Then

 MsgBox "Account disabled"

End If



If vFlag AND 8 Then

 MsgBox "User must change pw"

End If



If vFlag AND 16 Then

 MsgBox "User cannot change pw"

End If

To set these values yourself, you would use the OR operator.


'Assume variable vFlag already has a flag byte in it



'Turn on account disabled

vFlag = vFlag OR 4



'Force password change

vFlag = vFlag OR 8

You'll use this type of math a lot when dealing with ADSI. I introduce ADSI in Chapter 14, and start working with user accounts in Chapter 16. If this business of using Boolean operators to set values seems confusing, you're right; it is. Consider the OR operator, which is the one you'll use the most to set values.

Imagine that vFlag in the preceding example starts out with a value of zero. If you were to expand that out into binary, you'd get eight bits, all set to zero.


00000000

Using the code vFlag = vFlag OR 4 tries to combine whatever is in vFlag and the number 4. The number 4, written in binary, looks like this:


00000100

The first zero in that chain represents the value 128, the second represents 64, then 32, then 16, then 8, then 4, then 2, and then 1. So, the bit representing 4 is set to one, making the total value of the byte four.

The OR operator compares all the bits in vFlag with all the bits in 4.


00000000

00000100

OR always accepts two values and returns a 1 whenever either value contains a 1. So OR's output in this case would be


00000100

which means it simply returned a bit set to 1 whenever it encountered a 1 in either of the input values. Translating that to decimal, the result of 0 OR 4 is 4.

TIP

You can use Windows Calculator in Scientific mode to convert decimal numbers to binary-definitely a quicker way to do the conversion than doing it manually!


Following the example along, vFlag will now contain 4. The second operation is vFlag = vFlag OR 8. Let's convert both vFlag and 8 to binary to see how the OR operator will handle them.


00000100

00001000

That's vFlag-which currently contains 4-on the top, and 8 on the bottom. OR will return a 1 whenever it encounters a 1 in either input, so the output will be


00001100

Windows Calculator tells me that that converts to 12 in decimal, so vFlag will not contain the value 12. But it's not really the number 12 that's important. In this case, vFlag is being used to represent user account settings, so each bit is really a little switch. The first switch, when turned on, disables the account. The second requires the user to change his password. By using the OR operator, you can flip each switch independently.

What if you want to turn a switch off, re-enabling a user account? Use the AND operator. Suppose that vFlag contains 12.


00001100

You want to flip off the switch that's represented by the value 4. You can't use OR, because OR can only turn bits on. Instead, you use AND: vFlag = vFlag AND 8. That's because AND will return a 1 only when both inputs are set to 1. If either input is set to 0, AND will return 0. Breaking down 8 into binary reveals the following.


00001000

Notice that the bit representing the value 4 is set to 0. So when AND compares vFlag and 251, you get the following.


00001100

00001000

Only one bit has a mismatch between the two, so the output will be


00001000

thus turning off the switch representing account disabled.

NOTE

These Boolean operators also play a role in logical comparisons. For example, If v = 1 OR v = 2 is a comparison that will result in True if variable v contains either 1 or 2. Similarly, the comparison If v=1 And v = 2 would never be true, because v cannot contain both 1 and 2 at the same time.


    Previous Section Table of Contents Next Section