# & vs. && for MATLAB

Tags:
1. Jun 19, 2014

### Maylis

Hello,

I am having some difficulties understanding the difference between the two and when I would want to use one over the other.

2. Jun 20, 2014

### Staff: Mentor

in java & represents bitwise anding 000&111 = 000 vs 010&011 = 010 ...

whereas && represents logical anding where you have two boolean expressions A and B then A&&B will be true only if A is true and B is true.

3. Jun 20, 2014

### kreil

In MATLAB, & is the logical elementwise AND operation for matrices.

&& is a short-circuited logical AND operator.

The short-circuited operators && and || can be quicker than & and | in expressions where the evaluation can end early. For example consider the expression
Code (Text):

A && B && C && D

which evaluates to TRUE only if A, B, C, and D are all TRUE. If A is FALSE, then MATLAB will not even evaluate B, C, or D. However, in the expression
Code (Text):

A & B & C & D

MATLAB will always evaluate A, B, C, and D to determine the result of the expression.

Last edited: Jun 20, 2014