1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<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.11">
<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">
<p>
<a name="Element-by-element-Boolean-Operators"></a>
<a name="Element_002dby_002delement-Boolean-Operators"></a>
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>
|