OR, AND Operator
Asked Answered
F

8

14

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

Fortify answered 17/5, 2010 at 10:15 Comment(1)
Do you mean boolean, or binary operation?Thyrse
T
23

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 bitwise counterparts, & and | are not escaping.

Hope that helps.

Tarr answered 17/5, 2010 at 10:21 Comment(1)
I think you mean the bitwise counterparts, not the boolean counterparts.Abad
A
18

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.

Abad answered 17/5, 2010 at 10:16 Comment(2)
According to MSDN Logical OR is | and Logical AND is &. Where as Conditional OR is || and Conditional AND is &&. Source: msdn.microsoft.com/en-us/library/6a71f45d.aspxLandlubber
Ugh, that's terrible wording. Why can't they just call it "Binary OR/AND"?Abad
G
2
if(A == "haha" && B == "hihi") {
//hahahihi?
}

if(A == "haha" || B != "hihi") {
//hahahihi!?
}
Gondola answered 17/5, 2010 at 10:18 Comment(0)
O
0

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

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

if (A && B)
{
    Console.WriteLine("And");
}
Oberammergau answered 17/5, 2010 at 10:19 Comment(5)
Your example would never reach the && evaluation unless A && B are false, in which case it would never pass the 2nd statement.Perk
@Perk - That's not true unless Console.WriteLine() does a thread abort I don't know about? :)Thyrse
LOL @James... perhaps he thought it was an if/else or something.Salazar
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...?!Tarr
Yes - sorry folks - the first answer was an if / else. I realised my mistake and corrected. Sorry, @Perk :-)Oberammergau
L
0

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
Latrice answered 17/5, 2010 at 10:22 Comment(0)
A
0

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.

Athletic answered 17/5, 2010 at 11:29 Comment(0)
A
-1

&& 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

Anemology answered 17/5, 2010 at 10:48 Comment(1)
Your last paragraph is incorrect. That would be true for bitwise and (&) but certainly not for boolean and.Abad
A
-1

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

Alicaalicante answered 17/5, 2010 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.