0

I've part of code which is checking input pins of lpt port, but using decimal values:

while (PortAccess.Input(889) == 120)

How to use this instruction with binary values?

for example while bit 3 of 00100100 is 0 then do something.

Elfoc
  • 3,649
  • 15
  • 46
  • 57

2 Answers2

1

See Convert.ToInt32(string value, int fromBase)

while((value & Convert.ToInt32("00000100", 2)) == 0)

Or since we know the third bit is for (2^2)

while((value & 0x0004) == 0)

is also a clear enough piece of code, I guess.

tafa
  • 7,146
  • 3
  • 36
  • 40
  • "Operator '&' cannot be applied to operands of type 'int' and 'bool' :( int newPortValue = PortAccess.Input(889); if (newPortValue & 0x0004==0) – Elfoc Oct 11 '11 at 08:29
  • Sorry, the problem is about operator precedence, I am reflecting this issue to my answer. Please notice the parentheses added. – tafa Oct 11 '11 at 08:36
0

Ok, so i've done this, because tafa solution wasn't working and i couldn't make it work:

   var PortValue = Convert.ToString(PortAccess.Input(889), 2).PadLeft(8, '0');
   PortV.Text = PortValue;
   while (PortV.Text[3].ToString() == "1")
   {
   //some code
   }

It's probably not good solution, but it's working ;)

Elfoc
  • 3,649
  • 15
  • 46
  • 57