1 <?xml version="1.0" encoding="UTF-8"?>
4 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
5 * Copyright (C) 2011 - DIGITEO - Michael Baudin
6 * Copyright (C) 2017 - Samuel GOUGEON
8 * Copyright (C) 2012 - 2016 - Scilab Enterprises
10 * This file is hereby licensed under the terms of the GNU GPL v2.0,
11 * pursuant to article 5.3.4 of the CeCILL v.2.1.
12 * This file was originally licensed under the terms of the CeCILL v2.1,
13 * and continues to be available under such terms.
14 * For more information, see the COPYING file which you should have received
15 * along with this program.
18 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
19 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns3="http://www.w3.org/1999/xhtml"
20 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
21 xmlns:scilab="http://www.scilab.org"
22 xml:id="bitget" xml:lang="en">
24 <refname>bitget</refname>
25 <refpurpose>Extracts from integers bits of given indices</refpurpose>
34 <title>Parameters</title>
40 Scalar, vector, matrix or hypermatrix of positive decimal or encoded integers.
48 Scalar, vector, matrix or hypermatrix of decimal or encoded integers in
49 <literal>[1, bitmax]</literal> where <literal>bitmax</literal> is the
50 maximal index of bits for the type of <varname>x</varname>: Indices of bits
51 to be extracted. The bit #1 is the lightest one (2<superscript>0</superscript>).
53 <tr><th>typeof(x)</th><th>bitmax</th><td>..</td><th>typeof(x)</th><th>bitmax</th></tr>
54 <tr align="center"><td>int8 </td><td>7</td> <td> </td><td>uint8</td><td>8</td></tr>
55 <tr align="center"><td>int16 </td><td>15</td><td> </td><td>uint16</td><td>16</td></tr>
56 <tr align="center"><td>int32 </td><td>31</td><td> </td><td>uint32</td><td>32</td></tr>
57 <tr align="center"><td>int64 </td><td>63</td><td> </td><td>uint16</td><td>64</td></tr>
58 <tr align="center"><td>decimal</td><td>1024</td><td> </td><td></td><td></td></tr>
67 Scalar, vector, matrix or hypermatrix of 0 and 1 of the type of
68 <varname>x</varname>. The sizes and contents of <varname>y</varname> are
73 If <varname>x</varname> is a scalar:
76 <varname>y</varname> has the sizes of <varname>pos</varname>
79 <literal>y(i,j,..)</literal> is the value of bit
80 #<literal>pos(i,j,..)</literal> of <varname>x</varname>.
87 If <varname>pos</varname> is a scalar:
90 <varname>y</varname> has the sizes of <varname>x</varname>
93 <literal>y(i,j,..)</literal> is the value of the bit
94 #<literal>pos</literal> of <literal>x(i,j,..)</literal>.
101 If <varname>x</varname> and <varname>pos</varname> are arrays with
102 identical sizes, the processing is element-wise:
105 <varname>y</varname> has the sizes of <varname>x</varname>
106 and <varname>pos</varname>
109 <literal>y(i,j,..)</literal> is the value of the bit
110 #<literal>pos(i,j,..)</literal> of <literal>x(i,j,..)</literal>.
119 <varname>y</varname> is a matrix with
120 <literal>length(x)</literal> rows and
121 <literal>length(pos)</literal> columns.
124 <literal>y(i,j)</literal> is the value of the bit
125 #<literal>pos(j)</literal> of <literal>x(i)</literal>.
137 <title>Description</title>
139 <literal>bitget()</literal> scans chosen bits of the binary representation of some
140 positive integers <varname>x</varname>. It returns 0 for bits down, and 1 for bits up.
143 The result has the sizes of <varname>x</varname> or of <varname>pos</varname> or
147 However, when both <varname>x</varname> and <varname>pos</varname> are non-scalar and
148 have mismatching sizes, the result <varname>y</varname> is a matrix ignoring the sizes
149 of <varname>x</varname>. Then, after reshaping <varname>y</varname> with
150 <literal>y = matrix(y, [size(x) -1])</literal>, the value of the bit #b of
151 <literal>x(i,..,k)</literal> is in <literal>y(i,..,k,b)</literal>.
155 <title>Examples</title>
156 <programlisting role="example"><![CDATA[
158 // The 2nd bit is 1 (starting from the end).
166 bitget(uint8(13),4:-1:1)
169 <emphasis role="bold">With arrays and encoded integers:</emphasis>:
171 <programlisting role="example"><![CDATA[
177 <emphasis role="bold">With big decimal integers > 2<superscript>52</superscript></emphasis>:
179 <programlisting role="example"><![CDATA[
180 x = sum(2 .^([7 16 18 19 25 52 70]-1))
181 bitget(x, [7 16 18 19 35 52 70 80])
184 --> x = sum(2 .^([7 16 18 19 25 52 70]-1))
188 --> bitget(x, [7 16 18 19 35 52 70 80])
190 Nan Nan 1. 1. 0. 1. 1. 0.
193 <emphasis role="bold">x and pos are arrays with mismatching sizes:</emphasis>
195 <programlisting role="example"><![CDATA[
203 bitget(x, [5 8 12 39])
206 --> bitget(x, [5 8 12 39])
216 <refsection role="see also">
217 <title>See Also</title>
218 <simplelist type="inline">
220 <link linkend="bitstring">bitstring</link>
223 <link linkend="dec2bin">dec2bin</link>
226 <link linkend="bitset">bitset</link>
229 <link linkend="bitand">bitand</link>
232 <link linkend="and_op">&</link>
236 <refsection role="history">
237 <title>History</title>
240 <revnumber>6.1</revnumber>
244 Positive unsigned integers are now accepted.
247 64 bits encoded integers are now accepted.
250 For decimal integers, bits with index in [53, 1024] can now be retrieved.
253 For decimal integers > 2<superscript>52</superscript>, querying bits
254 below the <literal>%eps</literal> relative accuracy now returns
255 <literal>NaN</literal> instead of 0.
258 It is now possible to retrieve several bits from each component of