dslreports logo
 
    All Forums Hot Topics Gallery
spc
Search similar:


uniqs
611

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

PToN

Premium Member

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

join:2001-03-10
Tustin, CA

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

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

PToN

Premium Member

Ha! if that would had been a snake, it would had bitten me...

Thanks Steve.!

cdru
Go Colts
MVM
join:2003-05-14
Fort Wayne, IN

cdru to Steve

MVM

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

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

typo in UI ... really

cdru
Go Colts
MVM
join:2003-05-14
Fort Wayne, IN

cdru

MVM

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

LOL..

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