Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

April 10, 2006 12:00 AM

Operators Are Leading Double Lives

Windows IT Pro
InstantDoc ID #49726
Rating: (0)
Downloads
49726.zip

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.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.