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="bitand" xml:lang="en">
22 <refname>bitand</refname>
23 <refpurpose>bitwise logical AND 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>bitand(u, v)</literal> computes and returns in <literal>w(i)</literal>
103 the bitwise AND conjunction of <literal>u(i)</literal> and <literal>v(i)</literal>
104 bits: Bits of <literal>w(i)</literal> set to 1 are met
105 in <literal>u(i)</literal> AND in <literal>v(i)</literal>. Otherwise,
108 With encoded integers, <literal>bitand(u, v)</literal> is equivalent
109 to <literal>u & v</literal>. However, <literal>u & v</literal>
110 demands that <literal>u</literal> and <literal>v</literal> have
111 the same inttype, while <literal>bitand(..)</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([54 107 34]') // binary representations
127 <screen><![CDATA[--> bitand(54, 107)
131 --> dec2bin([54 107 34]')
138 <programlisting role="example"><![CDATA[
139 // Between 2 simple rows with zeros and ones
142 bitand(u, v) // [0 0 0 1] 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 = bitand(u, int64(v)); type(z) // 1 : decimal representation
155 z = bitand(uint8(u), int8(v)); typeof(z) // uint8
156 z = bitand(uint8(u), int32(v)); typeof(z) // int32
158 // Usage with 2 matrices
163 bitand(u, v) // [ 0 0 0 1 ; 34 34 34 34 ] expected
165 // Usage with a distributed scalar:
166 bitand([1 2 4 8 9 10 12], 8) // == bitand([1 2 4 8 9 10 12], [8 8 8 8 8 8 8])
167 bitand(4, [1 2 4 8 9 10 12]) // == bitand([4 4 4 4 4 4 4], [1 2 4 8 9 10 12])
170 <programlisting role="example"><![CDATA[
171 // With encoded integers, bitand(u,v) and u & v are equivalent:
174 [bitand(u, v) ; u & v]
175 // ... but "&" demands operands having the same type:
176 u & 13 // mismatching int8- | decimal- encodings
178 <screen><![CDATA[--> u = int8([2 3 10]);
180 --> [bitand(u, v) ; u & v]
185 Undefined operation for the given operands.
186 check or define function %i_h_s for overloading.
189 <programlisting role="example"><![CDATA[
190 // Examples with big decimal integers:
192 u = sum(2 .^(600+[0 3 9 20 45])) // ~ 1.46D+194
193 bitand(u, 2^601) == 0 // True: The bit #601 is set to 0 in u
195 bitand(u, v) == 2^603 // True: the bit #603 is the only one common to u and v
196 bitand(u, 2^646-1) == 0 // True: 2^646-1 has theoretically bits #1 to #645 set to 1
197 // BUT -1/(2^646) is <<< %eps and is then ignored:
198 // 2^646 is actually used instead of 2^646-1.
199 // Now, 2^646 has bits #1 to #645set to 0. So the result.
200 bitand(u, 2^646-2^599)==u // %T: 2^646-2^599 has actually bits #599 to #645 set to 1
202 n = fix(log2(u)) // == 645: Index of the heaviest bit of u
203 bitand(u, u+2^(n-53)) == u // True: Addressing bits below the lightest one has no effect
204 bitand(u, u-2^(n-53)) == u // True: idem
207 <refsection role="see also">
208 <title>See also</title>
209 <simplelist type="inline">
211 <link linkend="and_op">&</link>
214 <link linkend="and">and</link>
217 <link linkend="bitor">bitor</link>
220 <link linkend="dec2bin">dec2bin</link>
224 <refsection role="history">
225 <title>History</title>
228 <revnumber>6.0</revnumber>
232 Extension to positive signed encoded integers
235 Extension to new int64 and uint64 inttypes
238 Operands of mixed types or inttypes are now accepted.
241 bitand(scalar, array) and bitand(array, scalar) are now supported.
244 Extension to decimal numbers greater than
245 2^32 and even than 2^52,
246 up to 1.80D+308 (== number_properties("huge")), for
247 the 52 heaviest encoded bits of their unsigned mantissa.