|, ||
Binary OR between integers. Logical OR over/between booleans and numbers
Syntax
Element-wise operator:
intMat = intA | intB tfMat = A | B
Scalared short-circuited evaluation:
tf = U || V if (U || V) then ... end if (U | V) then ... end while (U || V) then ... end while (U | V) then ... end
Arguments
- intA, intB
Arrays of encoded integers of any inttype.
intAandintBmust have the same sizes to be processed element-wise. IfintAorintBis a scalar, it is priorly replicated to the size of the other operand before processing.If
intAandintBhave not the same integer inttype, the operand with the smaller encoding is converted to the wider according to the following ranks: int8 < uint8 < int16 < uint16 < int32 < uint32 < int64 < uint64.- intMat
Array of encoded integers with the same sizes of
intAandintB, and of the wider inttype of both (see hereabove). For any indexi,intMat(i) = bitor(intA(i), intB(i))
intA | []and[] | intAreturn the boolean arrayintA~=%nan.- A, B
Arrays of booleans or of numbers: encoded integers of any inttype, real or complex numbers.
AandBmay have distinct types, and may be one or both sparse-encoded. The special case whereAandBare both encoded integers is excluded (see here-above).AandBmust have the same sizes to be processed element-wise. IfAorBis a scalar, it is priorly replicated to the size of the other operand before processing.- tfMat
Boolean array with the same sizes of
AandB. IfAorBis sparse-encoded, so istfMat.For any index
i,tfMat(i)is%Tif eitherA(i)orB(i)is%Tor not zero. Otherwise,tfMat(i)is set to%F.
A | []and[] | Areturn[].- U, V
Arrays of full or sparse-encoded booleans or of numbers of any types and encoding: any inttype of encoded integers, full or sparse-encoded real or complex numbers.
UandVmay have distinct data types, number of dimensions, and sizes.- tf
single boolean:
U || Vis equal toor(U) | or(V), without evaluatingor(V)ifor(U)is true (this is why the||operator is so-called short-circuited).See or() for the definition of the evaluation to true depending on type and encoding.
Description
- ||
For any boolean or numerical operands
UandV,G = U || Vis equivalent toif and(U) // none of U components is null or false G = %t // V is not assessed else G = and(V) endUandVmay have distinct sizes or numbers of dimensions.- The result is always a single boolean.
and(V)is evaluated only ifand(U)is false. This mainly avoids yielding errors when further operations withVare meaningless or prone to error whenand(U)is true. See examples.

||is useful mainly out ofif ..andwhile ..conditions. Otherwise, it is just equivalent to the simple|.- |
When the
|operator is used inside a logical condition tested by awhileor by anifcontrol structure, it is equivalent to the||operator (see above):U|Vis equivalent toand(U)|and(V), without assessingVifand(U)is true.Otherwise,
|has two different actions:- When both operands are encoded integers,
intA | intBis processed element-wise and yields an array of integers resulting from the bitwiseANDbetween corresponding components.
Comparison withbitor():intA | intBaccepts negative integers, whilebitor(intA, intB)does not.bitor(A,B)works bitwise with decimal-encoded integers. - Otherwise,
when operands are arrays of numbers or/and booleans, they are still processed element-wise. Null numbers being considered as booleans false, the result is an array of booleans.
- When both operands are encoded integers,
![]() |
![]() |
|
Examples
A = [0 1; 1 0]; // equivalent to [%f %t ; %t %f] B = [1 1; 0 0]; // equivalent to [%t %t ; %f %f] spA = sparse(A); spB = sparse(B); spbA = sparse(A<>0); spbB = sparse(B<>0); iA = int8(A); iB = int8(B); cA = A + 0*%i; cB = B + 0*%i; // | as bitwise (and elementwise) operation // ---------------------------------------- // Both operands and the result are arrays of encoded integers iA | iB Ai8 = int8([ -1, 1; 127, -128]); // Integer representation of Ai8: // [ 1111 1111, 0000 0001 ; // 0111 1111, 1000 0000 ] Bi8 = int8([-2, 0; 126, -127]); // Integer representation of Bi8: // [ 1111 1110, 0000 0000 ; // 0111 1110, 1000 0001 ] Ai8 | Bi8 // Integer promotion Ai16 = int16(Ai8); Bui32 = uint32(Bi8); r = Ai16 | Bui32, typeof(r) // | as logical elementwise operation // ---------------------------------- // Operands are arrays of booleans or of numbers. The result is an array of booleans A | B A | spB // Result is sparse encoded iA | spB // Result is sparse encoded cA | iB A | %nan // %nan is %t // Shorted and scalared | or ||: // ----------------------------- // Operands are arrays of booleans or of numbers. The result is a single boolean function res=foo() error("foo() shall not be called") endfunction // | (simple) is always shorted in any if's condition: if %T | foo() then // foo() is not called and this is not executed end // || is scalared and shorted in any boolean expression, even out of if or while ones: T = [-2 1]; T || foo() // T has only true (non zero) components, and so is "true as a whole". int8(T) || foo() // Therefore, foo() as right operand is not assessed / called. T+0*%i || foo()
Avoiding conditional errors in or out of if and
while conditions:
A = [ 1 3 -2 ; 4 -1 2] c = ~issquare(A) | det(A)~=0 // det(A) is evaluated despite ~issquare(A) is true (so c is true anyway) // But A should be square to be able to compute its det(). ==> ERROR // Now, we use the short-circuited || : // det(A) is NO LONGER computed, since ~issquare(A) is true => anyway c is true c = ~issquare(A) || det(A)~=0 // => NO ERROR // In an "if" (or "while") tested condition, | is equivalent to || if ~issquare(A) | det(A)~=0 // => NO ERROR B = A * A.' end
--> A = [ 1 3 -2 ; 4 -1 2] A = 1. 3. -2. 4. -1. 2. --> c = ~issquare(A) | det(A)~=0 det: Wrong type for input argument #1: Square matrix expected. --> c = ~issquare(A) || det(A)~=0 // => NO ERROR c = T --> if ~issquare(A) | det(A)~=0 // => NO ERROR > B = A * A.' > end B = 14. -3. -3. 21.
Constant polynomials or rationals can't be processed with | or ||:
p = 1 + 0*%z typeof(p) p | 1
--> p = 1 + 0*%z p = 1 --> typeof(p) ans = polynomial --> p | 1 Undefined operation for the given operands. check or define function %p_g_s for overloading.
See also
- or — logical OR over the elements of a boolean or numerical array
- bitor — bitwise logical OR between element-wise integers of 2 arrays
- and operator (&) — Binary AND between integers. Logical AND over/between booleans and numbers
- not ~ — (~) não lógico
- if — Execução condicional (significa "se então senão")
- while — palavra-chave da estrutura while (significa "enquanto...")
- overloading — capacidades de overloading ("sobrecarga") de exibições, funções e operadores
History
| Versão | Descrição |
| 6.0 | || operator added |
| Report an issue | ||
| << ndims | Funções Elementares | size >> |