<html lang="en"> <head> <title>Element-by-element Boolean Operators - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Boolean-Expressions.html#Boolean-Expressions" title="Boolean Expressions"> <link rel="next" href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators" title="Short-circuit Boolean Operators"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Element-by-element-Boolean-Operators"></a> <a name="Element_002dby_002delement-Boolean-Operators"></a> <p> Next: <a rel="next" accesskey="n" href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators">Short-circuit Boolean Operators</a>, Up: <a rel="up" accesskey="u" href="Boolean-Expressions.html#Boolean-Expressions">Boolean Expressions</a> <hr> </div> <h4 class="subsection">8.5.1 Element-by-element Boolean Operators</h4> <p><a name="index-element_002dby_002delement-evaluation-518"></a> An <dfn>element-by-element boolean expression</dfn> is a combination of comparison expressions using the boolean operators “or” (‘<samp><span class="samp">|</span></samp>’), “and” (‘<samp><span class="samp">&</span></samp>’), and “not” (‘<samp><span class="samp">!</span></samp>’), along with parentheses to control nesting. The truth of the boolean expression is computed by combining the truth values of the corresponding elements of the component expressions. A value is considered to be false if it is zero, and true otherwise. <p>Element-by-element boolean expressions can be used wherever comparison expressions can be used. They can be used in <code>if</code> and <code>while</code> statements. However, a matrix value used as the condition in an <code>if</code> or <code>while</code> statement is only true if <em>all</em> of its elements are nonzero. <p>Like comparison operations, each element of an element-by-element boolean expression also has a numeric value (1 if true, 0 if false) that comes into play if the result of the boolean expression is stored in a variable, or used in arithmetic. <p>Here are descriptions of the three element-by-element boolean operators. <dl> <dt><var>boolean1</var><code> & </code><var>boolean2</var><dd><a name="index-g_t_0026-519"></a>Elements of the result are true if both corresponding elements of <var>boolean1</var> and <var>boolean2</var> are true. <br><dt><var>boolean1</var><code> | </code><var>boolean2</var><dd><a name="index-g_t_007c-520"></a>Elements of the result are true if either of the corresponding elements of <var>boolean1</var> or <var>boolean2</var> is true. <br><dt><code>! </code><var>boolean</var><dt><code>~ </code><var>boolean</var><dd><a name="index-g_t_007e-521"></a><a name="index-g_t_0021-522"></a>Each element of the result is true if the corresponding element of <var>boolean</var> is false. </dl> <p>For matrix operands, these operators work on an element-by-element basis. For example, the expression <pre class="example"> [1, 0; 0, 1] & [1, 0; 2, 3] </pre> <p class="noindent">returns a two by two identity matrix. <p>For the binary operators, the dimensions of the operands must conform if both are matrices. If one of the operands is a scalar and the other a matrix, the operator is applied to the scalar and each element of the matrix. <p>For the binary element-by-element boolean operators, both subexpressions <var>boolean1</var> and <var>boolean2</var> are evaluated before computing the result. This can make a difference when the expressions have side effects. For example, in the expression <pre class="example"> a & b++ </pre> <p class="noindent">the value of the variable <var>b</var> is incremented even if the variable <var>a</var> is zero. <p>This behavior is necessary for the boolean operators to work as described for matrix-valued operands. </body></html>