PDA

View Full Version : [SOLVED:] How To Work With Boolean Values



Cyberdude
04-22-2007, 01:03 PM
I defined a single cell with the name ?Flags?, which can have numeric values from ?0? to ?7?.
The values of the ?Flags? cell have the following meaning:
???FlgA?..FlgB?FlgC????????Interpretation
???...0???0???.0??? (=0) ??FlgA & FlgB & FlgC = 0
???...0???0???.1??? (=1) ??FlgC = 1
???...0???1???.0??? (=2) ??FlgB = 1
???...0???1???.1??? (=3) ??FlgC & FlgB both = 1
???...1???0???.0??? (=4) ??FlgA = 1
???...1???0???.1??? (=5) ??FlgA & FlgC both = 1
???...1???1???.0??? (=6) ??FlgA & FlgB both = 1
???...1???1???.1??? (=7) ??FlgA & FlgB & FlgC = 1

My first problem is how to set the individual values of the bits in cell ?Flags? using VBA statements?
I do not want to fetch the current value of ?Flags? before changing the value of one of the bits, since that would reduce the efficiency of using a single cell to store a group of flag values. (Note that in practice there will actually be 16 bits.) I might add that the Msgbox statement uses this technique to handle the specification of the list of optional buttons.

How do I test the ?Flags? cell to determine the status of FlgA, FlgB, and FlgC?
For example, I want to write the equivalent of the following IF statement:


If Range(?FlgA?) = 1 And Range(?FlgB?) = 0 And Range(?FlgC?) = 1 Then . . .

Presumably, since the combination ?101? is the binary value of ?5?, I could write:


If Range(?Flags?) = 5 Then . . .

But if I want to write the statement:


If Range(?FlgA?) = 1 And Range(?FlgB?) = 0 Then . . .

I could write either of the following and be correct because I?m testing only the leftmost 2 digits to see if they are ?10?:


If Range(?Flags?) = 4 Then . . .
If Range(?Flags?) = 5 Then . . .

So which one should I write?? Is there a better way?

JimmyTheHand
04-22-2007, 02:41 PM
Hi :hi:

I'm not sure I interpret the questions well, but these things below may be of help
You can retrieve individual flags like this:

FlgA = (Flags And 4) /4
FlgB = (Flags And 2) / 2
FlgC = (Flags And 1)
In other words, you can check individual flags like this:

Check FlgA: If Flags And 4 = 4 Then ...
Check FlgB: If Flags And 2 = 2 Then ...
Check FlgC: If Flags And 1 = 1 Then ...
Building the "Flags" from individual flag values

Flags = FlgC + FlgB*2 + FlgA*4 ...

Changing FlgC to True False
Flags = Flags Or 1 Flags = Flags Not 1

Changing FlgB to True False
Flags = Flags Or 2 Flags = Flags Not 2
etc...
HTH
Jimmy

EDIT
I realized just now, thanks to John in post #3, that I had mistakenly exchanged FlgA and FlgC. Now it's corrected.

johnske
04-22-2007, 02:51 PM
You have 3 flags with 8 possible binary states (0 to 7) and thus 8 possible courses of action - just sum them, e.g. in your nomenclature


BinarySum = Range(“FlgA”)*4 + Range(“FlgB”)*2 + Range(“FlgC”)
Select Case BinarySum
Case 1,2
'do something
Case 3
'do something else
Case 4
'etc
'''
End Select

Cyberdude
04-23-2007, 07:51 PM
Thanks, guys. I'll start working with that info. I figured it would be something like what you showed me, but I just couldn't get my hands around it.
Thanx again!
Sid