September 8, 2010   Register  Login  
    Search 
View Article

Current Articles | Categories | Search | Syndication

Monday, October 03, 2005
Bitfields Page 2
By Joey Davis @ 8:37 PM :: 2226 Views ::
Now, back to the shipping example. Let’s take a minute to look at 1) how to manually calculate the bitfield value based off of the options selected and 2) how to manually determine the selected options based off of the bitfield value. 

Going From Your Options To The Bitfield Value
Based on the first table, if your client has a customer named ‘Cust1’, and your client ships to them on Sunday and Monday, then the value in the bitfield would be 3. Here is how the value (3) is calculated manually: Sunday (the 0 bit) has a value of 1 (20). Monday (the 1 bit) has a value of 2 (21). The value for the bitfield is calculated as {Sunday = 20 = 1} + {Monday = 21 = 2} OR 1 + 2 = 3. If ‘Cust1’ ships on Tuesday and Thursday, then the value in the bitfield would be 20. Here is how the value (20) is calculated manually: Tuesday (the 2 bit) has a value of 4 (22). Thursday (the 4 bit) has a value of 16 (24). The value for the bitfield is calculated as {Tuesday = 22 = 4} + {Thursday = 24 = 16} OR 4 + 16 = 20. 

Backing Into Your Options From The Bitfield Value
Next, let’s take 2 sample bitfield values and figure out which options were set to derive that value in the first place. First, take the value 38: the options that result in a value of 38 are Friday (32) and Tuesday (4) and Monday (2) {32 + 4 + 2 = 38}. Next, take the value 19: the options that result in a value of 19 are Thursday (16) and Monday (2) and Sunday (1) {16 + 2 + 1 = 19}. KEY CONCEPT: think about the following magical mathematical fact: the 2x formula ensured that any given bitfield value will have only one possible combination of options that result in that value. Soak on this for a minute while I repeat myself. When the option values are derived by raising the ‘2’ to the power of the bit number, then the bitfield value will ALWAYS be such that 1) all possible combinations are accounted for AND 2) each value has only one possible combination of options that resolve to that value. Take a second to come up with your own number (it has to be less than or equal to the highest option value). Here is how to determine which options resolve to your value:


1) Find the option with the highest value that is equal to or less than your bitfield value. You just found the first option. 
2) Subtract the option value from the bitfield value.
3) Repeat the process until you reach zero

Here Is An Example : 38 (Using the Days of the Week table above)
1) Highest value that is less than or equal to 38 is 32 (Friday)
2) Subtract 32 from 38 and we are left with 6
3) Next highest value equal to or less than 6 is 4 (Tuesday)
4) Subtract 4 from 6 and we are left with 2
5) Next highest value that is less than or equal to 2 is 2 (Monday)
6) Subtract 2 from 2 and we are done!

Using VBA Operators To Accomplish The Goal
Now that you know how to calculate the bitfield value manually, the good news is that you’ll never have to do this (in your code, that is). VBA has provided an operator for ‘combining’ multiple values into a single value, the ‘Or’ operator. Also, there is an operator to testing a value to see which options are set, the ‘And’ operator. VBA also has an operator (^) for raising a number to the power of another number {In VBA 27 is written as 2^7}. In order to use the VBA operators, all you really have to know is which option belongs with which bit.
 
Previous Page | Next Page
Comments
Currently, there are no comments. Be the first to post one!
Click here to post a comment

  Copyright 2005 by accesswizards.com   Terms Of Use | Privacy Statement