Bitwise comparison and optional criteria

Banana

split with a cherry atop.
Local time
Today, 15:16
Joined
Sep 1, 2005
Messages
6,318
As you may know, we can do a bitwise comparison for criteria or flags.

This works well if all criteria are required; so if we required 'a, b and d', we can ask thus:
Code:
If (MyCriteria And a) And _
     (MyCriteria And b) And _
     (MyCriteria and d) Then
     '.... Criteria passed
Else
     '.... Criteria wasn't satisifed
End If

But suppose we wanted to require '(either a or b) and d'? It surely could expressed as thus:
Code:
If (MyCriteria And a) Or _
     (MyCriteria And b) And _
     (MyCriteria And d) Then
     '.... Criteria passed
Else
     '.... Criteria wasn't satisifed
End If

But the goal is to make the criteria easy to set up so we can just enter a value and somehow know that only either one of a given pair is required so instead of a lengthy If and And/Ors, we can do just this:

Code:
If MyCriteria And ConstantValue Then

Is it possible to express the optional requirement in a bitwise comparison, or is there a better approach that will make it easy to alter the criteria required for a given context?

I thought about assigning multiple acceptable values for a given criteria from a lookup table so if we wanted to express '(a or b) and d', we can have two rows of "a Or d" and "a Or b", but that requires careful maintenance of all possible values and requires checking all possible combinations every time.

Also considered using a extra bit as a switch to indicate "either this or that" or "that and this" but felt too restrictive and a bigger headache.

I also can't assign same values to both a and b because for a different set criteria, it could be '(b or c) and d' while another criteria could be 'a and b and c and d'.

So... suggestions or ideas are welcome.
 
G’day Banana.

You may be able to work backwards on this (That’s if I’m reading it correctly.)

Rather than start with the data, and try to apply that data to a formula, you might be able to list the formula, in a table, and then ask for user data entry based on the formula. You should then be able to Eval() the user selected formula with the user entered data based on the formula they selected.

There are some analogue and digital formula attached.

Hope I’m reading your question and intensions correctly.

Regards,
Chris.
 

Attachments

Oh, fiddlesticks!

Stupid vBulletin software said:
You must spread some Reputation around before giving it to ChrisO again.

Anyway, the sample was quite excellent. While I would have not preferred to use Eval() functions, it was invaluable in giving me ideas of writing out some generic functions, basically reproducing Ors and Ands, but taking arguments which I can then pipe together to express the logic needed for the given context.

Thanks!!
 
with a byte consisting of 8 flags (ie potentially eight 1s) the flags are represented by bits 1, etc thru 8

the numeric representation of each bit is therefore

1,2,4,8,16,32,64,128

so if we want to test bits 2 and 5 (say) ie values 2 and 16, we can just say with a logical and

allbitsset = mybyte AND 18 =18 (ie the sum of the bit values 2 and 16)

and if we want to test whether bits 2 or 5 are set then the easiest way is to use the OR operator

so in the above

anybitset = mybyte OR 18 > 0

since we arent worried about the actual value - just whether any of the positions was a "hit"#

{make sure the order of preference for operators is correct

ie anybitset = mybyte OR 18 > 0 should work as (mybyte OR 18) > 0


[editied - this is wrong - see my later post - it should still be AND, not OR]
 
Last edited:
Gemma is quite correct its a bit like working out the permutations of doubles and trebles, etc in a multi selection bet. If you give each entity a unique number, as suggested, 2,4,8,16, etc by adding any combination of the numbers will give you a unique combination.

David
 
i think i'm wrong with the OR operator though

i posted that off the top of my head - i've had a play now

to test for a choice of bits, its still AND, not OR

so to test for all bits,

its allbitsset = mybyte AND 18 = 18

to test for any bits,

its anybitsset = mybyte AND 18 > 0

-------------
out of interest the XOR bit operation is useful for trivial encryption

encoded = mybyte XOR codepattern returns an encrypted value

and

normal= encoded XOR codepattern

returns to the original unencrypted form

useful to hide stuff form casual inspection, without being realy obvious
 

Users who are viewing this thread

Back
Top Bottom