Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hi

I'm trying to do a logical AND between two integers (x && y) but I get the following error:
Operator && cannot be applied to operands of type 'int'

I get a similar error when converting the integers to byte.

Is there another operator/function I could use or to what should I convert the integers to be able to do the logical AND?

Posted

1 solution

C# doesn't use (int)1 as true, so you will need to use (x==y && y==1)
see the link below, but to make a long story short, the C# compiler tends
to be more like a binary child, it likes things in strong typed format,
as such if there is any disention inside its ranks, it will just throw a compilation error.
just try something like

int a = Math.Round(Math.PI,0);


which throws Error:

Cannot implicitly convert type 'double' to 'int'.<br />An explicit conversion exists (are you missing a cast?)

and can be resolved by

int a = (int)Math.Round(Math.PI,0);


but you cannot use:
<br /> int a = 2, b = 3;<br /> if((bool)(a)&&(bool)(b)){}//Does NOT work<br />


alternativly you can restructure to just use bools

<br />      bool bx;    //     int ix;<br />      bx = false; //     ix = 0;<br />      bx != bx;   //     ix = ix % 2;<br />




or make an operator
<br />BinaryAnd(int x,int y)<br />{<br />     if(x==1&&y==1){return true;}<br />     return false;<br />}


you should say more about how you intend to use it,
because there are lots of ways to get the same result.


clicky...hope it helps, my C is a bit rusty, it's been a couple of years. [confused]
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900