views:

177

answers:

8

Newbie question. How to calculate the value of the formula A f B, where f - the binary function OR or AND?

Thanks for answers :)

+6  A: 

Logical OR is ||, logical AND is &&. If you need the negation NOT, prefix your expression with !.

Example:

X = (A && B) || C || !D;

Then X will be true when either A and B are true or if C is true or if D is not true (i.e. false).

If you wanted bit-wise AND/OR/NOT, you would use &, | and ~. But if you are dealing with boolean/truth values, you do not want to use those. They do not provide short-circuit evaluation for example due to the way a bitwise operation works.

ThiefMaster
A: 
if(A == "haha" && B == "hihi") {
//hahahihi?
}

if(A == "haha" || B != "hihi") {
//hahahihi!?
}
Younes
A: 

I'm not sure if this answers your question, but for example:

if (A || B)
{
    Console.WriteLine("Or");
}

if (A && B)
{
    Console.WriteLine("And");
}
pm_2
James
@James - That's not true unless `Console.WriteLine()` does a thread abort I don't know about? :)
Nick Craver
LOL @James... perhaps he thought it was an if/else or something.
Lirik
If A or B is true and the other is false the console output would be "Or". If A and B are true, the console output would be "Or", "And". Otherwise nothing. Not sure what the talk about thread abort is all about...?!
Martin Randall
The answer was edited it was an if/else.
James
Yes - sorry folks - the first answer was an if / else. I realised my mistake and corrected. Sorry, @James :-)
pm_2
@pm_2: thanks for clearing that up thought I was going mad...
James
+5  A: 

There is a distinction between the conditional operators && and || and the boolean operators & and |. Mainly it is a difference of precendence (which operators get evaluated first) and also the && and || are 'escaping'. This means that is a sequence such as...

cond1 && cond2 && cond3

If cond1 is false, neither cond2 or cond3 are evaluated as the code rightly assumes that no matter what their value, the expression cannot be true. Likewise...

cond1 || cond2 || cond3

If cond1 is true, neither cond2 or cond3 are evaluated as the expression must be true no matter what their value is.

The boolean counterparts, & and | are not escaping.

Hope that helps.

Martin Randall
I think you mean the **bitwise** counterparts, not the *boolean* counterparts.
ThiefMaster
A: 

Use '&&' for AND and use '||' for OR, for example:

bool A;
bool B;

bool resultOfAnd = A && B; // Returns the result of an AND
bool resultOfOr = A || B; // Returns the result of an OR
Ben
A: 

&& it's operation return true only if both operand it's true which implies

bool and(bool b1, bool b2)]
{
 if(b1==true)
 {
   if(b2==true)
    return true;
 }
 return false;
}

|| it's operation return true if one or both operand it's true which implies

bool or(bool b1,bool b2)
{
 if(b1==true)
 return true;
 if(b2==true)
 return true;
 return false;
}

if You write

y=45&&34//45 binary 101101, 35 binary 100010

in result you have

y=32// in binary 100000

Therefore, the which I wrote above is used with respect to every pair of bits

Andriy Mytroshyn
ThiefMaster
A: 

If what interests you is bitwise operations look here for a brief tutorial : http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx .bitwise operation perform the same operations like the ones exemplified above they just work with binary representation (the operation applies to each individual bit of the value)

If you want logical operation answers are already given.

Olorin
A: 

many answers above, i will try a different way:

if you are looking for bitwise operations use only one of the marks like:

3 & 1 //==1 - and 4 | 1 //==5 - or

Zéiksz