1 <?xml version="1.0" encoding="UTF-8"?>
4 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
5 * Copyright (C) 2016 - Samuel GOUGEON
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
17 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
18 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns3="http://www.w3.org/1999/xhtml"
19 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
20 xmlns:scilab="http://www.scilab.org" xml:id="bitxor" xml:lang="en">
22 <refname>bitxor</refname>
23 <refpurpose>bitwise logical XOR between element-wise integers of 2 arrays</refpurpose>
32 <title>Parameters</title>
38 scalars, vectors, matrices or hypermatrices of null or
39 positive integers encoded as decimal or integer numbers
40 of any signed or unsigned <link linkend="inttype">inttype</link>.
41 <warning>Sparse-encoded matrices are not accepted.</warning>
44 If <literal>u</literal> and <literal>v</literal> have
45 the same type and inttype, this one is the working one.
49 if <literal>u</literal> or <literal>v</literal>
50 is decimal-encoded, the working inttype is 0
51 (real decimal), even if the other operand
52 is int64- or uint64-encoded.
55 if <literal>u</literal> and <literal>v</literal>
56 are both encoded integers, the working inttype
57 is the widest of both: int8 < uint8 <
58 int16 < uint16 < int32 < uint32 <
64 The result <literal>w</literal> gets the type of the
68 <literal>u</literal> and <literal>v</literal> are
69 processed element-wise:
72 If <literal>u</literal> is a single value (scalar)
73 and <literal>v</literal> is a vector, matrix or
74 hypermatrix, <literal>u</literal> is priorly
75 expanded as <literal>u*ones(v)</literal> in order
76 to operate <literal>u</literal> with every
77 <literal>v</literal> component.
80 Conversely, <literal>v</literal> is priorly
81 expanded as <literal>v*ones(u)</literal> if it
85 If neither <literal>u</literal> nor <literal>v</literal>
86 are scalars, they must have the same sizes.
91 The result <literal>w</literal> gets the sizes of
92 <literal>u</literal> or/and <literal>v</literal>
100 <title>Description</title>
101 For each pair of components <literal>u(i)</literal> and <literal>v(i)</literal>,
102 <literal>bitxor(u, v)</literal> computes and returns in <literal>w(i)</literal>
103 the bitwise XOR eXclusive-OR conjunction of <literal>u(i)</literal> and
104 <literal>v(i)</literal>
107 With encoded integers, <literal>bitxor(u,v)</literal> is equivalent
108 to <literal>(u | v) & ~(u & v)</literal>.
109 However, both <literal>|</literal> and <literal>&</literal>
110 operators demand that <literal>u</literal> and <literal>v</literal>
111 have the same inttype, while <literal>bitxor(..)</literal> accepts
115 For any decimal integer <literal>u</literal> greater than 2^52,
116 only its bits from log2(u) down to log2(u)-52 are encoded and can
117 be actually taken into account. Lower bits are not stored and are
122 <title>Examples</title>
123 <programlisting role="example"><![CDATA[
125 dec2bin([25 33 56]') // binary representations
127 <screen><![CDATA[--> bitxor(25, 33)
131 --> dec2bin([25 33 56]'))
138 <programlisting role="example"><![CDATA[
139 // Between 2 simple rows with zeros and ones
142 bitxor(u, v) // [0 1 1 0] expected
144 // Encoded integers such as int8 are accepted:
149 // Operands of mixed types are accepted.
150 // The type of the result is decimal if a decimal operand is involved,
151 // or the widest integer one otherwise:
154 z = bitxor(u, int64(v)); type(z) // 1 : decimal representation
155 z = bitxor(uint8(u), int8(v)); typeof(z) // uint8
156 z = bitxor(uint8(u), int32(v)); typeof(z) // int32
158 // Usage with 2 matrices
163 bitxor(u, v) // [ 3 4 8 24 ; 56 56 33 25 ] expected
165 // Usage with a distributed scalar:
166 bitxor([1 2 4 8 9 10 12], 8) // == bitxor([1 2 4 8 9 10 12], [8 8 8 8 8 8 8])
167 bitxor(4, [1 2 4 8 9 10 12]) // == bitxor([4 4 4 4 4 4 4], [1 2 4 8 9 10 12])
170 <programlisting role="example"><![CDATA[
171 // Examples with big decimal integers:
173 u = sum(2 .^(600+[0 3 9 20 45])) // ~ 1.46D+194
174 bitxor(u, 2^630) == u+2^630 // true: XOR sets to 1 the missing bit #630 of u, so adds it
175 bitxor(u, 2^645) == u-2^645 // true: XOR sets to 0 the existing bit #645 of u, so removes it
176 bitxor(u, 2^601) == u // false: The bit #601 is 0 in u. XOR changes it.
178 n = fix(log2(u)) // 645 == Index of the heaviest bit of u
179 bitxor(u, 2^(n-52)) == u // false: The lightest bit of u was at 0 => This changes it
180 bitxor(u, 2^(n-53)) == u // true: Addressing bits below the lightest one doesn't change u
183 <refsection role="see also">
184 <title>See also</title>
185 <simplelist type="inline">
187 <link linkend="or_op">|</link>
190 <link linkend="or">or</link>
193 <link linkend="bitor">bitor</link>
196 <link linkend="bitand">bitand</link>
199 <link linkend="dec2bin">dec2bin</link>
203 <refsection role="history">
204 <title>History</title>
207 <revnumber>6.0</revnumber>
211 Extension to positive signed encoded integers
214 Extension to new int64 and uint64 inttypes
217 Operands of mixed types or inttypes are now accepted.
220 bitxor(scalar, array) and bitxor(array, scalar) are now supported.
223 Extension to decimal numbers greater than
224 2^32 and even than 2^52,
225 up to 1.80D+308 (== number_properties("huge")), for
226 the 52 heaviest encoded bits of their unsigned mantissa.