Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert byte to bool in C#

I'm trying make a library to iot core on c# based on arduino library (http://blog.electrodragon.com/rc522-write-a-card-demo-code/) but i don't understand what is:

if (!(temp & 0x03))

or

while ((i!=0) && !(n&0x01) && !(n&waitIRq))

It's required boolean values but those are bytes! How i can convert this?

If anyone know a library already made, please let me know. Thank you.

like image 387
marcos Avatar asked Nov 04 '25 00:11

marcos


1 Answers

In C any non-zero expression is implicitly true, and zero is false. In C# you need to do an explicit comparison:

if ((temp & 0x03) == 0)

and

while ((i!=0) && (n&0x01)==0 && (n&waitIRq)==0)

Alternatively, you can use the .NET's Boolean structure:

bool isTempAppropriate = (temp & 0x03) == 0;
if(isTempAppropriate) { ... }

Note that bool is just syntactic sugar for System.Boolean, and that you could have used the var keyword instead of bool.

like image 63
Konamiman Avatar answered Nov 06 '25 15:11

Konamiman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!