Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


April 10, 2006

Operators Are Leading Double Lives

RSS
Subscribe to Windows IT Pro | See More Tips Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events 7 Ways To Get More From Your SharePoint Deployment Now

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement