Click to See Complete Forum and Search --> : Question with & Operator


Maverick0821
March 7th, 2003, 07:31 AM
I know in C you used to be able to do the following:

int i=3;
int j=1;

if (i&j)
{
//We know the 1st bit was set in i
}

However, the above gives me a syntax error in C#. How do I do that here?

bfarley
March 7th, 2003, 11:30 AM
Unlike C, the exp in an if statement must return a boolean, the & operation does not

Maverick0821
March 7th, 2003, 11:46 AM
I knew that i&j returns an int, however I was looking for something equivalent to this in C# that returns a boolean. Anbody with any ideas?

pareshgh
March 7th, 2003, 12:01 PM
try this

[1]
bool b = (i&j) >= 1 ? true : false ;
if ( b)
{
}

or

[2]
if ( ( ( (i&j) >= 1 ) ? true : false) )
{

}

bfarley
March 7th, 2003, 12:36 PM
bool b = i&j wont work either. Cant cast an int to bool

Bill F

pareshgh
March 7th, 2003, 12:40 PM
Ah actually

you need to keep brackets

bool b = ((i&j) >= 1) ? true : false ;

or add brackets when needed. since
if's now need strictly bools so...

Paresh