Open In App

How To Use | and || Operator in MATLAB?

Improve
Improve
Like Article
Like
Save
Share
Report

In MATLAB, | and || are both logical operators that are used to perform logical OR operations on Boolean variables, however, there is a subtle difference between the two:

  • |
  • || 

The element-wise logical OR operator “|” takes two arrays of the same size and returns an array of the same size where each element is the result of the logical OR operation between the corresponding elements of the input arrays. For example, if A and B are two arrays of the same size, then A|B will return a new array C of the same size, where each element of C is the result of the logical OR operation between the corresponding elements of A and B. 

The | operator is a bitwise OR operator, which compares the binary representation of two values and returns a new value where each bit is set to 1 if either or both of the corresponding bits in the original values are 1. For example:

Matlab




A = [1 0 0; 1 1 1];
B = [1 1 0; 0 1 0];
C = A | B;


In this example, C will be equal to [1 1 0; 1 1 1], because each element of C is the bitwise OR of the corresponding elements in A and B.

The short-circuit logical OR operator “||” takes two scalar inputs and returns a scalar output. It operates by first evaluating the first input. If the first input is true, then the operator returns true without evaluating the second input. If the first input is false, then the operator evaluates the second input and returns its value.

The || operator is a logical OR operator, which compares two values and returns a Boolean value of true if either or both of the values are true. For example:

Matlab




A = [1 0 0; 1 1 1];
B = [1 1 0; 0 1 0];
C = A || B;


In this example, C will be equal to [1 1 0; 1 1 1], because each element of C is the logical OR of the corresponding elements in A and B.

As a general rule, it is recommended to use the || operator when working with logical expressions, and the | operator when working with bitwise operations.

Example 1:

Matlab




A = 5;  % binary representation of 5 is 101
B = 3;  % binary representation of 3 is 011
result = A | B;
disp(result);


Output:

 

In this example, the binary representation of A is 101 and the binary representation of B is 011. When using the bitwise OR operator |, it compares each bit of the binary representation of A and B.
1 OR 0 = 1, 0 OR 1 = 1, 1 OR 1 = 1.
So, the result will be 101 (5) | 011 (3) = 111 (7).

Example 2:

Matlab




A = true;
B = false;
result = A || B;
disp(result);


Output:

 

In this example, we are using the logical OR operator || to check if either A or B is true. This program sets the value of variable A to true, which is equivalent to the boolean value 1, and the value of variable B to false, which is equivalent to the boolean value 0. The program then uses the logical OR operator || to perform a logical OR operation on the two boolean values. Since the OR operator returns true if at least one of the operands is true, the result of the operation, in this case, is true, which is equivalent to the numerical value 1. Finally, the program uses the disp function to display the value of the result variable, which is 1.

It’s worth mentioning that the logical OR operator || will only evaluate the second operand if the first operand is false, this is known as short-circuit evaluation.

Example 3:

Matlab




year = 2020; % replace with your desired year
if mod(year, 4) == 0 && (mod(year, 100) ~= 0 || mod(year, 400) == 0)
    fprintf('%d is a leap year. \n', year);
else
    fprintf('%d is not a leap year. \n', year);
end


Output:

 

Explanation:

This code uses the modulus operator mod to check whether the year is evenly divisible by 4. If it is, the code then checks whether it is evenly divisible by 100. If it is not, then it is a leap year. If it is evenly divisible by 100, the code then checks whether it is evenly divisible by 400. If it is, it is a leap year; if it is not, it is not a leap year. The fprintf function is used to print a message indicating whether the year is a leap year or not.



Last Updated : 16 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads