Skip navigation

Boolean Arithmetic with Hexadecimal Values

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
Hide comments

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.
Publish