Skip navigation

Operators Are Leading Double Lives


Did you know that VBScript's logical operators—AND, OR, NOT, and XOR—are leading double lives? They can perform not only logical comparisons against text, numbers, and dates but also bit comparisons. This capability shouldn't come as a shock to anyone, but it's often overlooked.

Does being able to perform bit comparisons have any practical purpose? You bet it does. The Windows registry and a lot of other applications use bits for configuration values. EvenVBScript's MsgBox function uses them.

Bits are one of the rare feats of human engineering in that they're easier to put together than to take apart. For example, the NoDrives entry in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer registry key uses a 32-bit REG_DWORD value to hide drives from Windows Explorer. In this case, A=1, B=2, C=4, D=8, E=16, and so on, with each following letter's value being twice the one before it. To hide drives B, D, and E, you simply add their values together:

2 + 8 + 16 = 26 

Like I said, putting them together is easy.

Now let's say you have a REG _DWORD value of 16,793,600 in the NoDrives entry. To figure out which drives are being hidden, you have to figure out which powers of two (e.g., 21, 22, 23, 24) add up to that value. The manual way would be to find the closest power of two that's equal to or smaller than the REG_DWORD value and subtract it from the REG_DWORD value. If the result isn't 0, you find the closest power of two to the result and subtract it, and so on until the result is 0. For the example of 16,793,600, the manual calculation would be

16,793,600 - 16,777,216 = 16,384 16,384 - 16,384 = 0 

So, there are only two drives hidden: drives O and Y. As you can see, if there were more drives hidden, this manual calculation would become quite tedious.

Enter the AND operator. This operator can tell you the bit values that exist in a decimal or hex number. You don't have to worry about any binary conversions—the AND operator takes care of that for you. I wrote BitComp.vbs, which Listing 1, page 15, shows, to take advantage of this capability. This script derives individual bit values from any decimal or hex number you enter in the GUI that Figure 1 shows. To designate a hex number, you need to preface it with the letter H, which is case insensitive (e.g., h4EF4).

You don't need to modify Bit-Comp.vbs at all to use it. You just need to have Windows Script Host (WSH) 1.0 or later installed on your computer. Note that BitComp.vbs can't determine bit values for decimal values larger than 2,147,483,647 or hex values larger than 7FFFFFFF. Alternatively, you can use the Get-Bits function, which callout A in Listing 1 shows, or the Hex2Dec function, which callout B in Listing 1 shows, in other scripts. You just need to pass in the number you want to evaluate as an argument.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.