Author |
|
GadgetGuy Super User
Joined: June 01 2008 Location: United States
Online Status: Offline Posts: 942
|
Posted: November 08 2022 at 15:30 | IP Logged
|
|
|
I can’t believe I am asking this, especially after using PH for almost 3 decades, but I have tried for two days to figure out a way to take a series of binary numbers and OR them in ph but cannot.
I have a macro with lot’s of possible solution outcomes and rather than reporting each and every decision as it happens via the UserMessage and cluttering up the Event Log I would like to build a binary result and then output a single EventLog message at the end of the macro..
Thus I would like to construct a binary mask for each outcome such as (for example)…
0001 >> lights turned on
0010 >> lights turned off
0100 >> sound toggled
1000 >> system turned off
Etc.
And then OR them together as the various tests are completed so in the end I get a simple binary output such as “0101” indicating that the lights were turned on and the sound was togged on/off.
I have tried everything I can think of involving numbers, strings, using ‘OR’ and ‘ph_OR” and even addition but nothing works, All I can figure out is that PH will not “do” Boolean math.
Am I missing something, or is there a way?
GaddgetGuy
Edited by GadgetGuy - November 08 2022 at 15:32
__________________ Ken B - Live every day like it's your last. Eventually, you'll get it right!
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: November 08 2022 at 20:57 | IP Logged
|
|
|
Ken,
Take a look at the ph_and(), ph_or(), and ph_xor() functions for bitwise functions.
Do everything initially in decimal and at the very end, output as binary with the d2b() function.
So you could do something like this:
d2h(ph_or(ph_or(ph_or(ph_or(0,if(lightson,1,0)),if(lightsoff ,2,0)),if(soundtoggled,4,0)),if(systemturnedoff,8,0)),4)
Since you're dealing with binary numbers, I think it would be cleaner to just do math like this:
d2h(if(lightson,1,0) + if(lightsoff,2,0) + if(soundtoggled,4,0) + if(systemturnedoff,8,0),4)
If the "lightson", etc were variables that were holding a 1 if true and 0 if false, then this would be the simplest:
d2h(lightson * 1 + lightsoff * 2 + soundtoggled * 4 + systemturnedoff * 8,4)
Hope this helps,
Dave.
|
Back to Top |
|
|
|
|