将代码段从C++转换为C#

Ada*_*one 0 c# c++ .net-micro-framework

我试图将这段c ++代码转换为c#:

  if (latch_state & 0x1) {
    MC->setPin(AIN2pin, HIGH);
  } else {
    MC->setPin(AIN2pin, LOW);
  }

  if (latch_state & 0x2) {
    MC->setPin(BIN1pin, HIGH);
  } else {
    MC->setPin(BIN1pin, LOW);
  }

  if (latch_state & 0x4) {
    MC->setPin(AIN1pin, HIGH);
  } else {
    MC->setPin(AIN1pin, LOW);
  }

  if (latch_state & 0x8) {
    MC->setPin(BIN2pin, HIGH);
  } else {
    MC->setPin(BIN2pin, LOW);
  }
Run Code Online (Sandbox Code Playgroud)

我知道足以转换MC->setPin(PinNum, state)MC.setPin(pinNum, state)所以它们不是问题,但我对if语句将成为什么感到困惑.

latch_state是类型uint8_t,但似乎处理像一个字节(这是我试图将其转换为),并且0x1似乎也是一个字节.

那么二元和操作如何在if语句中进行求值?

对于转换,我应该这样做

if ((latch_state & 0x1) == 0x0)或者if ((latch_state & 0x1) != 0x0)完全不同的东西?

Dav*_*jas 7

if ((latch_state & 0x1) != 0)
Run Code Online (Sandbox Code Playgroud)

应该管用.通常,不评估为布尔值的C++条件与0进行隐式比较,"true"表示表达式不为0.