dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
583
share rss forum feed


PToN
Premium
join:2001-10-04
Houston, TX

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


Steve
I know your IP address
Consultant
join:2001-03-10
Foothill Ranch, CA
kudos:5
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

--
Stephen J. Friedl | Unix Wizard | Security Consultant | Orange County, California USA | my web site


PToN
Premium
join:2001-10-04
Houston, TX
Ha! if that would had been a snake, it would had bitten me...

Thanks Steve.!


cdru
Go Colts
Premium,MVM
join:2003-05-14
Fort Wayne, IN
kudos:7
reply 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
join:2001-10-04
Houston, TX
reply to PToN
My little app is almost done..!

Thanks to both of you.

dave
Premium,MVM
join:2000-05-04
not in ohio
kudos:8

1 recommendation

typo in UI ... really


cdru
Go Colts
Premium,MVM
join:2003-05-14
Fort Wayne, IN
kudos:7
said by dave:

typo in UI ... really

No, it's just really the relay reading.


PToN
Premium
join:2001-10-04
Houston, TX
reply to PToN
LOL..

It's on "v1"... it is expected... hahaha