views:

244

answers:

7

I know the rules for && but what is & and | how to evaluate? Please explain me with an example.

A: 

Those are bitwise operators. This should help:

http://www.ensta.fr/~diam/java/online/notes-java/data/expressions/bitops.html

Evan Mulawski
+18  A: 

Those are the bitwise AND and bitwise OR operators.

int a = 6; // 110
int b = 4; // 100

// Bitwise AND    

int c = a & b;
//   110
// & 100
// -----
//   100

// Bitwise OR

int d = a | b;
//   110
// | 100
// -----
//   110

System.out.println(c); // 4
System.out.println(d); // 6

EDIT

Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs.

Indeed when both inputs are boolean, the operators are considered the Boolean Logical Operators and behave similar to the Conditional-And (&&) and Conditional-Or (||) operators except for the fact that they don't short-circuit so while the following is safe:

if((a != null) && (a.something == 3)){
}

This is not:

if((a != null) & (a.something == 3)){
}
Justin Niessner
giddy
@giddy correct.
Jonathon
@giddy @Jonathon - I updated my values to better show that situation.
Justin Niessner
incomplete: they are also LOGICAL operators (for booleans).
Carlos Heuberger
@Carlos- No. They're still bitwise operators. They just behave the same as non-short circuiting logical operators. There is a difference.
Justin Niessner
Carlos Heuberger
@Carlos - Good call. Thanks for pointing out the appropriate spot in the specification. I updated my answer.
Justin Niessner
You can improve the answer further by illuminating on "this is safe" and "this is not safe." It's better to have a method call (which will have a "side affect") instead of accessing a property to show the later could be unsafe.
Jaywalker
A: 

The operators && and || are short-circuiting, meaning they will not evaluate their right-hand expression if the value of the left-hand expression is enough to determine the result.

Tassos Bassoukos
-1 for telling the OP what he already knew and not answering the question he actually asked.
Alnitak
+1  A: 

& and | are bitwise operators on integral types (e.g. int): http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

&& and || operate on booleans only (and short-circuit, as other answers have already said).

Bruno
A: 

& and | provide the same outcome as the && and || operators. The difference is that they always evaluate both sides of the expression where as && and || stop evaluating if the first condition is enough to determine the outcome.

Brian Scott
Same outcome -for booleans- ...
KarlP
The Elite Gentleman
Brian Scott
Piskvor
Carlos Heuberger
@Carlos Heuberger: I stand corrected.
Piskvor
@Brian Scott: if you had qualified your answer with *for booleans* as added by KarlP, it would be a very good answer. I still upvoted it to balance the unjustified downvotes.
seanizer
@seanizer, much appreciated.
Brian Scott
A: 

Check out short circuiting.

&& and || won't check the second argument if the first argument is enough to work out the outcome.

E.g.

bool something = true;
bool anotherThing = false;
if (something && anotherThing)
//or
if (something || anotherThing)

in the above the anotherThing variable will not be evaluated because something = true so the overall evaluation is true.

When you use & or |, all arguments are evaluated, regardless if the overall result can be found from the first argument.

Fermin
Would the downvoters explain what their problem is? This answer may not be complete, but it certainly is correct.
seanizer
+8  A: 

I think you're talking about the logical meaning of both operators, here you have a table-resume:

boolean a, b;

Operation     Meaning                       Note
---------     -------                       ----
   a && b     logical AND                    short-circuiting
   a || b     logical OR                     short-circuiting
   a &  b     boolean logical AND            not short-circuiting
   a |  b     boolean logical OR             not short-circuiting
   a ^  b     boolean logical exclusive OR
  !a          logical NOT

short-circuiting        (x != 0) && (1/x > 1)   SAFE
not short-circuiting    (x != 0) &  (1/x > 1)   NOT SAFE
Torres
Great answer (the best one IMHO), but I fixed your formatting
seanizer
Thank you for the edit @seanizer!
Torres