Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


June 01, 1999

Boolean Arithmetic with Hexadecimal Values


RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    Oops
Main Article    An ADSI Primer, Part 6: Using ADSI to Create and Manipulate User Accounts

Assume that you want an attribute of an object (e.g., userFlags of the User object) to set 8 values. You use an 8-bit binary number to represent those 8 values. If you want the attribute to hold 11 values, you use an 11-bit binary number.

The binary system is a base-2 system in which 0 typically represents a false condition and 1 typically represents a true condition. In this example, that means 0 represents the value isn't set and 1 represents the value is set. So, if you want to set only the third and eighth values of an 8-value attribute, you set the third and eighth bits of an 8-bit binary number to 1, or &B10000100. (You read binary numbers from right to left.) The prefix &B specifies that the number is binary.

However, attributes store data as decimal values. Thus, you need to convert the binary number into a decimal value, which is base 10. For example, the binary number &B10000100 translates into

27 + 22
128 + 4 = 132

Instead of manually translating binary numbers into decimal values, you can use a conversion table, such as the Numeric Equivalents table in the Microsoft Press Computer Dictionary.

Although binary numbers are the foundation for attributes and attributes store information as decimal values, constants are typically hex values. The hex system is a base-16 system that uses a combination of digits and letters to represent values. You use the prefix &H to specify that a value is hex. For example, a conversion table reveals that the hex value for the decimal value of 132 is &H84.

Where does the Boolean arithmetic fit in? You use the Boolean And operator to check whether a bit is set and the Or operator to set a bit. (If you're unfamiliar with these operators, see Tim Hill, Windows NT Shell Scripting, Macmillan Technical Publishing.)

For example, suppose you want to see whether the fourth bit is set in an 8-bit binary number that has a decimal value of 132. You can check for the existence of this bit using the And operator in a binary equation:

&B10000100 And &B00001000 = &B00000000

You solve this equation by calculating each bit individually. For example, the first bit in &B10000100 is 0 and the first bit in &B00001000 is 0; 0 And 0 is 0. The second bit in &B10000100 is 0, and the second bit in &B00001000 is 0; 0 And 0 is 0. The third bit in &B10000100 is 1, and the third bit in &B00001000 is 0; 1 And 0 is 0. When you calculate all eight bits, the result is &B00000000. In other words, the fourth bit isn't set.

Suppose you want to test whether the third bit is set:

&B10000100 And &B0000100 = &B00000100

Because the third bit in &B10000100 is 1 and the third bit in &B0000100 is 1, the resulting bit is 1 (1 And 1 is 1), which specifies that the value for the third bit is set.

Let's translate this binary equation into decimal and hex equations:

&B10000100 And &B0000100 = &B00000100
132 And 4 = 4
&H84 And &H4 = &H4

If the return value is 0 or &H0, the bit isn't set. If the return value is the bit's actual value (in this case, 4 or &H4), the bit is set.

Just like the And operator, the Or operator works with binary, decimal, and hex systems. Taking the example just given, let's try to set the third bit, which happens to be already set:

&B10000100 OR &B0000100 = &B10000100
132 OR 4 = 4
&H84 OR &H4 = &H84

In other words, the result is the new value with that bit set. Because that bit was already set, nothing changes. Let's try setting the fourth bit, which isn't already set:

&B10000100 OR &B00001000 = &B10001100
132 OR 8 = 140
&H84 OR &H8 = &H8C

The result includes a newly set fourth bit. You can even set two bits at once. For example, here's how you set the fourth and fifth bits:

&B10000100 OR &B00011000 = &B10011100
132 OR 24 = 156
&H84 OR &H18 = &H9C

Although the Boolean mathematics is straightforward, you luckily don't have to include this code in a script. Instead, you typically use constants. For example, if you declare the constant

Const UF_DONT_EXPIRE_PASSWD = &H10000

you just need to specify that constant in the script. So to determine this bit's existence, you use the code

If intUserFlags And UF_DONT_EXPIRE_PASSWD = 0 Then
'UF_DONT_EXPIRE_PASSWD is not set
Else
'UF_DONT_EXPIRE_PASSWD is set
End If

You set bits in a similar fashion. For example, to set the &H10000 bit, you use the code

intUserFlags = intUserFlags Or UF_DONT_EXPIRE_PASSWD

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Accessing Database Data with ADO

...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

WinInfo Short Takes: Week of September 1, 2008

An often irreverent look at some of the week's other news, including a very late short takes, screwy Microsoft patents, AT&T's competitors are smelling blood in the water, Nintendo keeps rocking and rolling, Dell, Steve Jobs not-so-RIP, and so much more ...


Related Events 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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Increase Application Performance
Free White Paper by Editor's Best winner, Texas Memory Systems.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
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 Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing