Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome General
 PowerHome Messageboard : PowerHome General
Subject Topic: Binary/Boolean Math? Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: November 08 2022 at 15:30 | IP Logged Quote GadgetGuy

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 View GadgetGuy's Profile Search for other posts by GadgetGuy
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: November 08 2022 at 20:57 | IP Logged Quote dhoward

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 View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum