Algorithm needed

Can someone please point me to where I can find an algorithm which options have been selected when those options are summed, as in the Messagebox options MB_YESNO+MB_ICONQUESTION.

Thanks for your help.

Ron

Subject: Algorithm needed.

To construct the results you can use either simple addition or a binary OR. (Not a logical

OR.)

“4 or 2” always yields True

but

“4 | 2” alwyas yields 6 because

4 is 0000 0100 binary

2 is 0000 0010 binary

The binary OR principle returns non-zero when either or both compared bit is non-zero.

Hence

6 is 0000 0110

Collin

KC8TKA

Subject: RE: Algorithm needed.

Collin, thanks for your response, but I am not understanding something here.

I have options:

Option A = 1

Option B = 2

Option C = 4

Option D = 8

Option E = 16

If the user selects options A + B + D that totals 11. Is there an algorithm which I can use to see if an option was selected, i.e. OptionC = <<function(…)>>

or do I just use brute force and work backwards, i.e.

if iOptions >= 16 then

  OptionE = True

  iOptions = iOptions - 16

end if

if iOptions >= 8 then

  OptionD = True

  iOptions = iOptions - 8

end if

…etc…

Thanks.

Ron

Subject: RE: Algorithm needed.

No, although I think you’ll find AND more useful than OR for this – AND 16 will return 16 if the 16 option was selected, 0 otherwise.

Subject: RE: Algorithm needed.

Stan,

Thanks so much. Not knowing what I was doing, I was really trying to make it complicated by creating a math formula, or converting to binary, or whatever.

Thanks very much for your help.

Ron

Subject: RE: Algorithm needed.

Again, binary.if a & b will yield a not-false result if the matching bits are non-zero.

if a = 0000 0110 and b = 0000 0010

a & b = 0000 0010, which is a non-zero result, telling you that the bit is set.

OptionA = 0000 0001

OptionB = 0000 0010

OptionC = 0000 0100

OptionD = 0000 1000

OptionE = 0001 0000

CheckValue = 0001 0010

Comparison

if CheckValue & OptionC

gives a 0 result, so the option is not set.

Collin

Subject: RE: Algorithm needed.

Collin,

Thanks so much. Not knowing what I was doing, I was really trying to make it complicated by creating a math formula, or converting to binary, or whatever. I now understand what you were trying to explain to me. I really didn’t think it could be that easy.

Thanks very much for your help.

Ron