简体   繁体   中英

Translating some bit operations from javascript to c# — if(int & #)

So I'm translating some java to c# here is a example bit

// bits = 12 bit number
int bits = table[index];

if (bits & 1)
{...}

if (bits & 2)
{...}

if (bits & 3)
{...}

ect

the bits & # bit errors because of

Cannot implicitly convert type 'int' to 'bool'

I understand the error, i'm just not sure how to convert the bits & # bit to c# safe code, anyone know how it should be?

Thank you.

Easy, just compare the result with 0.

Some C based languages treat 0 as false and other values as true. But that is not true for c#

if ((bits & 2) !=0 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM