radbyx
radbyx

Reputation: 9650

Bitwise operation in C#. How to convert 2 long's to a bool result?

How can I check if "1" is in "9" in C#?

long l1 = 1L; // 0001
long l9 = 9L; // 1001
if (l1 & l9) // True (Cannot implicit convert 'long' to 'bool)
{
}

It's possible with "&" in JavaScript and in vb it's "And", but I just can't figure out what I'm missing here.

Upvotes: 2

Views: 1004

Answers (1)

Tigran
Tigran

Reputation: 62248

// check if result of binary op is != 0
// that means "contains" 
if ((l1 & l9) != 0)
{
   ...
} 

You need to check if the result of the operation is not equal 0.

EDIT As @Damien: correctly noted, in this case would correct to check against inequality to 0, as simple >0 comparison may produce false positives if 63th bit is somehow involved.

Upvotes: 6

Related Questions