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) 2012 - 2016 - Scilab Enterprises
7 * Copyright (C) 2017 - 2020 - Samuel GOUGEON
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"
21 xml:id="bitget" xml:lang="en">
23 <refname>bitget</refname>
24 <refpurpose>Extracts from integers bits of given indices</refpurpose>
33 <title>Parameters</title>
38 Scalar, vector, matrix or hypermatrix of positive decimal or encoded integers.
45 Scalar, vector, matrix or hypermatrix of decimal or encoded integers in
46 <literal>[1, bitmax]</literal> where <literal>bitmax</literal> is the
47 maximal index of bits for the type of <varname>x</varname>: Indices of bits
48 to be extracted. The bit #1 is the lightest one (2<superscript>0</superscript>).
50 <tr><th>typeof(x)</th><th>bitmax</th><td>..</td><th>typeof(x)</th><th>bitmax</th></tr>
51 <tr align="center"><td>int8 </td><td>7</td> <td> </td><td>uint8</td><td>8</td></tr>
52 <tr align="center"><td>int16 </td><td>15</td><td> </td><td>uint16</td><td>16</td></tr>
53 <tr align="center"><td>int32 </td><td>31</td><td> </td><td>uint32</td><td>32</td></tr>
54 <tr align="center"><td>int64 </td><td>63</td><td> </td><td>uint64</td><td>64</td></tr>
55 <tr align="center"><td>decimal</td><td>1024</td><td> </td><td></td><td></td></tr>
64 Scalar, vector, matrix or hypermatrix of 0 and 1 of the type of
65 <varname>x</varname>. The sizes and contents of <varname>y</varname> are
69 <emphasis role="bold">If <varname>x</varname> is a scalar</emphasis>:
72 <varname>y</varname> has the sizes of <varname>pos</varname>
75 <literal>y(i,j,..)</literal> is the value of bit
76 #<literal>pos(i,j,..)</literal> of <varname>x</varname>.
81 <emphasis role="bold">If <varname>pos</varname> is a scalar</emphasis>:
84 <varname>y</varname> has the sizes of <varname>x</varname>
87 <literal>y(i,j,..)</literal> is the value of the bit
88 #<literal>pos</literal> of <literal>x(i,j,..)</literal>.
93 <emphasis role="bold">If <varname>x</varname> and <varname>pos</varname>
94 are arrays with identical sizes</emphasis>, the processing is element-wise:
97 <varname>y</varname> has the sizes of <varname>x</varname>
98 and <varname>pos</varname>
101 <literal>y(i,j,..)</literal> is the value of the bit
102 #<literal>pos(i,j,..)</literal> of <literal>x(i,j,..)</literal>.
107 <emphasis role="bold">Otherwise</emphasis>:
110 <varname>y</varname> is a matrix with
111 <literal>length(x)</literal> rows and
112 <literal>length(pos)</literal> columns.
115 <literal>y(i,j)</literal> is the value of the bit
116 #<literal>pos(j)</literal> of <literal>x(i)</literal>.
125 <title>Description</title>
127 <literal>bitget()</literal> scans chosen bits of the binary representation of some
128 positive integers <varname>x</varname>. It returns 0 for bits down, and 1 for bits up.
131 The result has the sizes of <varname>x</varname> or of <varname>pos</varname> or
135 However, when both <varname>x</varname> and <varname>pos</varname> are non-scalar and
136 have mismatching sizes, the result <varname>y</varname> is a matrix ignoring the sizes
137 of <varname>x</varname>. Then, after reshaping <varname>y</varname> with
138 <literal>y = matrix(y, [size(x) -1])</literal>, the value of the bit #b of
139 <literal>x(i,..,k)</literal> is in <literal>y(i,..,k,b)</literal>.
143 <title>Examples</title>
144 <programlisting role="example"><![CDATA[
146 // The 2nd bit is 1 (starting from the end).
154 bitget(uint8(13),4:-1:1)
157 <emphasis role="bold">With encoded integers</emphasis>:
159 <programlisting role="example"><![CDATA[
161 x = sum(int16(2).^(b-1))
167 --> b = [1 3 8 11 15];
168 --> x = sum(int16(2).^(b-1))
172 --> B = bitget(x, 1:15)
174 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1
185 <emphasis role="bold">
186 With uint64 integers > 2<superscript>52</superscript>
189 <programlisting role="example"><![CDATA[
190 b = [1 12 23 34 45 53 64];
191 x = sum(uint64(2).^(b-1))
197 --> b = [1 12 23 34 45 53 64];
198 --> x = sum(uint64(2).^(b-1))
202 --> B = bitget(x, 1:64)
205 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
208 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
212 1. 12. 23. 34. 45. 53. 64.
219 <emphasis role="bold">
220 With big decimal integers > 2<superscript>52</superscript>
223 <programlisting role="example"><![CDATA[
224 x = sum(2 .^([7 16 18 19 25 52 70]-1))
225 bitget(x, [7 16 18 19 35 52 70 80])
228 --> x = sum(2 .^([7 16 18 19 25 52 70]-1))
232 --> bitget(x, [7 16 18 19 35 52 70 80])
234 Nan Nan 1. 1. 0. 1. 1. 0.
237 <emphasis role="bold">x and pos are arrays with mismatching sizes:</emphasis>
239 <programlisting role="example"><![CDATA[
247 bitget(x, [5 8 12 39])
250 --> bitget(x, [5 8 12 39])
260 <refsection role="see also">
261 <title>See Also</title>
262 <simplelist type="inline">
264 <link linkend="bitstring">bitstring</link>
267 <link linkend="dec2bin">dec2bin</link>
270 <link linkend="bitset">bitset</link>
273 <link linkend="bitand">bitand</link>
276 <link linkend="and_op">&</link>
280 <refsection role="history">
281 <title>History</title>
284 <revnumber>6.1</revnumber>
288 Positive signed integers are now accepted.
291 64 bits encoded integers are now accepted.
294 For decimal integers, bits with index in [53, 1024] can now be retrieved.
297 For decimal integers > 2<superscript>52</superscript>, querying bits
298 below the <literal>%eps</literal> relative accuracy now returns
299 <literal>NaN</literal> instead of 0.
302 It is now possible to retrieve several bits from each component of