PToN Premium Member join:2001-10-04 Houston, TX |
PToN
Premium Member
2012-Jun-14 12:10 pm
C# and Reed Relays and Bit counting??Hello,
I have this reed relay with 4 outputs. #1 = 1, #2 = 2, #3 = 4 and #4 = 8.
When i turn them all ON, the int returned is 15. I am trying to think of a way to read their status and provide a user friendly reading of which relay is ON and which one is OFF.
I am really lost here.
I basically need to know which relay is one and which is off.
I could start with something like:
If INT is odd Relay #1 is ON Else Something i dont know.
Maybe someone has a link where i can learn about this would be helpful.
Thanks |
|
SteveI know your IP address
join:2001-03-10 Tustin, CA |
Steve
2012-Jun-14 12:28 pm
int bits = <get the value>;
if ( bits & 1 ) relay #1 is on
if ( bits & 2 ) relay #2 is on
if ( bits & 4 ) relay #3 is on
if ( bits & 8 ) relay #4 is on
Steve |
|
PToN Premium Member join:2001-10-04 Houston, TX |
PToN
Premium Member
2012-Jun-14 12:42 pm
Ha! if that would had been a snake, it would had bitten me...
Thanks Steve.! |
|
|
cdruGo Colts MVM join:2003-05-14 Fort Wayne, IN |
to Steve
said by Steve:int bits = <get the value>;
if ( bits & 1 ) relay #1 is on
if ( bits & 2 ) relay #2 is on
if ( bits & 4 ) relay #3 is on
if ( bits & 8 ) relay #4 is on
Steve You'll need to slightly modify that for C# as you'll get an error about converting type int to bool. if ((bits & 1) == 1)...
if ((bits & 2) == 2)...
if ((bits & 4) == 4)...
if ((bits & 8) == 8)...
|
|
PToN Premium Member join:2001-10-04 Houston, TX |
PToN
Premium Member
2012-Jun-14 5:06 pm
My little app is almost done..! Thanks to both of you. |
|
dave Premium Member join:2000-05-04 not in ohio
1 recommendation |
dave
Premium Member
2012-Jun-16 5:15 pm
typo in UI ... really |
|
cdruGo Colts MVM join:2003-05-14 Fort Wayne, IN |
cdru
MVM
2012-Jun-16 5:55 pm
said by dave:typo in UI ... really No, it's just really the relay reading. |
|
PToN Premium Member join:2001-10-04 Houston, TX |
PToN
Premium Member
2012-Jun-18 10:36 am
LOL..
It's on "v1"... it is expected... hahaha |
|