1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Mapping Function} {@var{z} =} xor (@var{x}, @var{y})
21 ## Return the `exclusive or' of the entries of @var{x} and @var{y}.
22 ## For boolean expressions @var{x} and @var{y},
23 ## @code{xor (@var{x}, @var{y})} is true if and only if one of @var{x} or
24 ## @var{y} is true. Otherwise, for @var{x} and @var{y} both true or both
25 ## false, @code{xor} returns false.
27 ## The truth table for the xor operation is
29 ## @multitable @columnfractions 0.44 .03 .05 .03 0.44
30 ## @item @tab @var{x} @tab @var{y} @tab @var{z} @tab
31 ## @item @tab 0 @tab 0 @tab 0 @tab
32 ## @item @tab 1 @tab 0 @tab 1 @tab
33 ## @item @tab 0 @tab 1 @tab 1 @tab
34 ## @item @tab 1 @tab 1 @tab 0 @tab
37 ## @seealso{and, or, not}
40 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
41 ## Created: 16 September 1994
44 function z = xor (x, y)
47 if (isscalar (x) || isscalar (y) || size_equal (x, y))
48 ## Typecast to logicals is necessary for other numeric types.
49 z = logical (x) != logical (y);
52 z = bsxfun (@xor, x, y);
54 error ("xor: X and Y must be of compatible size or scalars");
63 %!assert((xor ([1, 1, 0, 0], [0, 1, 0, 1]) == [1, 0, 0, 1]
64 %! && xor ([i, i, 0, 0], [1, 0, 1, 0]) == [0, 1, 1, 0]));
66 %!assert(all (all (xor (eye (2), fliplr (eye (2))) == ones (2))));
70 %!error xor (1, 2, 3);