<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2020 - Samuel GOUGEON
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns3="http://www.w3.org/1999/xhtml"
      xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
      xmlns:scilab="http://www.scilab.org" xml:id="bitcmp" xml:lang="en">
    <refnamediv>
        <refname>bitcmp</refname>
        <refpurpose>bitwise complement of integers</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            y = bitcmp(x)
            y = bitcmp(x, bitnum)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x, y</term>
                <listitem>
                    arrays of signed or unsigned integers (int8, .., uint64), or of positive decimal
                    integers. Hypermatrix supported. <varname>x</varname> and <varname>y</varname>
                    have the same integer type and the same size.
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>bitnum</term>
                <listitem>
                    positive encoded or decimal integer, or array of positive encoded or decimal
                    integers of size equal to size(x): bits #1 to #bitnum of <varname>x</varname>
                    are considered and inversed. According to the integer type of
                    <varname>x</varname>, <varname>bitnum</varname> must be in the interval
                    <literal>[1, bitmax]</literal> with bitmax as follows:
                    <table>
                        <tr><th>inttype :</th>
                            <td>int8</td>
                            <td>uint8</td>
                            <td>int16</td>
                            <td>uint16</td>
                            <td>int32</td>
                            <td>uint32</td>
                            <td>int64</td>
                            <td>uint64</td>
                            <td>decimal</td>
                        </tr>
                        <tr><th>bitmax :</th>
                            <td>8</td>
                            <td>8</td>
                            <td>16</td>
                            <td>16</td>
                            <td>32</td>
                            <td>32</td>
                            <td>64</td>
                            <td>64</td>
                            <td>1024</td>
                        </tr>
                    </table>
                    <para/>
                    The default <varname>bitnum</varname> value depends on the input integer
                    type:
                    <itemizedlist>
                        <listitem>
                            <literal>bitnum = bitmax</literal> for encoded integers
                        </listitem>
                        <listitem>
                            <literal>bitnum = 53</literal> for decimal integers
                            <literal>x ≤ 1/%eps</literal>
                        </listitem>
                        <listitem>
                            <literal>bitnum = int(log2(x))+1</literal> for decimal integers
                            <literal>x > 1/%eps</literal>.
                        </listitem>
                    </itemizedlist>
                    <para/>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            <literal>bitcmp(x)</literal> computes the binary complement of each element of
            <varname>x</varname>, and provides it in the corresponding element of <varname>y</varname>.
        </para>
        <para>
            In the following description, 2^0 corresponds to the bit #1. An integer whose highest
            bit on is #n, is in <literal>[2^(n-1), 2^n-1]</literal>.
        </para>
        <para>
            For <varname>x</varname> such that <literal>abs(x) ≥ 2^bitnum</literal>,
            its bits <literal>#(bitnum+1)</literal> to <literal>#(int(log2(x))+1)</literal>
            are ignored. Only its bits <literal>#1</literal> to <literal>#bitnum</literal>
            are considered and inversed.
        </para>
        <para>
            Example:
            <screen>
--> bitget(180, 1:8)
 ans  =
   0.   0.   1.   0.   1.   1.   0.   1.
--> bitcmp(180, 4)
 ans  =
   11.
--> bitget(11, 1:8)
 ans  =
   1.   1.   0.   1.   0.   0.   0.   0.
</screen>
        </para>
        <para>
            For <varname>x</varname> such that <literal>abs(x) &lt; 2^bitnum</literal>,
            bits <literal>#(int(log2(x))+2)</literal> to <literal>#bitnum</literal> are padded
            with 0. Then all bits <literal>#1</literal> to <literal>#bitnum</literal> are considered
            and inversed.
        </para>
        <para>
            Example:
            <screen>
--> x = 30; int(log2(30))+2
 ans  =
   6.
--> bitget(30, 1:10)
 ans  =
   0.   1.   1.   1.   1.   0.   0.   0.   0.   0.
--> bitcmp(30, 7)
 ans  =
   97.
--> bitget(97, 1:10)
 ans  =
   1.   0.   0.   0.   0.   1.   1.   0.   0.   0.
</screen>
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
x = uint8(13);
b = bitget(x, 1:8)
c = bitcmp(x, 8)
bitget(c, 1:8)
1 - b
   ]]></programlisting>
        <screen><![CDATA[
--> b = bitget(x, 1:8)
 b  =
  1  0  1  1  0  0  0  0

--> c = bitcmp(x, 8)
 c  =
  242

--> bitget(c, 1:8)
 ans  =
  0  1  0  0  1  1  1  1

--> 1 - b
 ans  =
  0  1  0  0  1  1  1  1
]]></screen>
        <para/>
        <para>
        Negative encoded integers are accepted:
        </para>
        <programlisting role="example"><![CDATA[
bitcmp(int8([-71 -34 -1 0 33 70]))
bitcmp(int8([-71 -34 -1 0 33 70]), 8)
bitcmp(int8([-71 -34 -1 0 33 70]), 7)
bitcmp(int8([-71 -34 -1 0 33 70]), 6)
bitcmp(int8([-71 -34 -1 0 33 70]), 5)
bitcmp(int8([-71 -34 -1 0 33 70]), 4)
     ]]></programlisting>
        <screen><![CDATA[
--> bitcmp(int8([-71 -34 -1 0 33 70]))
 ans  =
  70  33  0 -1 -34 -71

--> bitcmp(int8([-71 -34 -1 0 33 70]), 8)
 ans  =
  70  33  0 -1 -34 -71

--> bitcmp(int8([-71 -34 -1 0 33 70]), 7)
 ans  =
  70  33  0  127  94  57

--> bitcmp(int8([-71 -34 -1 0 33 70]), 6)
 ans  =
  6  33  0  63  30  57

--> bitcmp(int8([-71 -34 -1 0 33 70]), 5)
 ans  =
  6  1  0  31  30  25

--> bitcmp(int8([-71 -34 -1 0 33 70]), 4)
 ans  =
  6  1  0  15  14  9
]]></screen>
        <para/>
        <para>
            We can work with 64-bit big integers:
        </para>
        <programlisting role="example"><![CDATA[
b = (rand(1,62)<0.5)*1;
x = sum(b .* (uint64(2).^(0:61)))
r = bitcmp(x)
bg = bitget(r, 1:62);
[msprintf("%d ",b(:)) ; msprintf("%d ",bg(:))]
     ]]></programlisting>
        <screen><![CDATA[
--> x = sum(b .* (uint64(2).^(0:61)))
 x  =
  4154509482123930814

--> r = bitcmp(x)
 r  =
  14292234591585620801

--> bg = bitget(r, 1:62);
--> [msprintf("%d ",b(:)) ; msprintf("%d ",bg(:))]
 ans  =
  "0 1 1 1 1 1 0 1 0 0 1 1 0 1 0 0 ... 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 1 1 0 0 1 1 1 "
  "1 0 0 0 0 0 1 0 1 1 0 0 1 0 1 1 ... 0 1 1 1 1 0 1 1 0 0 0 0 0 1 1 0 1 0 0 1 1 0 0 0 "
]]></screen>
        <para/>
        <para>
        bitnum can be an array:
        </para>
        <programlisting role="example"><![CDATA[
bitcmp([0 0 0 0 0], 3:7)
     ]]></programlisting>
        <screen><![CDATA[
--> bitcmp([0 0 0 0 0], 3:7)
 ans  =
   7.   15.   31.   63.   127.
]]></screen>
        <para/>
        <para>
            bitnum can be > 52:
        </para>
        <programlisting role="example"><![CDATA[
format(22)
bitcmp(2^70, 65)
sum(2.^(13:64))   // 52 highest bits
     ]]></programlisting>
        <screen><![CDATA[
--> bitcmp(2^70, 65)
 ans  =
   36893488147419095040.

--> sum(2.^(13:64))
 ans  =
   36893488147419095040.
]]></screen>
        <para/>
        <para>
            Huge decimal numbers can be processed:
        </para>
        <programlisting role="example"><![CDATA[
format(22)
log2(1e100)
r = bitcmp(1e100, 333)
bitcmp(1e100) // bitnum = int(log2(x)) + 1 is used by default
bitcmp(r, 333)
     ]]></programlisting>
        <screen><![CDATA[
--> log2(1e100)
 ans  =
   332.19280948873625903

--> r = bitcmp(1e100, 333)
 r  =
   7.498005798264093D+99

--> bitcmp(1e100)  // bitnum = int(log2(x)) + 1 is used by default
 ans  =
   7.498005798264093D+99

--> bitcmp(r, 333)
 ans  =
   1.00000000000000D+100
]]></screen>
    </refsection>
    <refsection role="see also">
        <title>See Also</title>
        <simplelist type="inline">
            <member>
                <link linkend="bitxor">bitxor</link>
            </member>
            <member>
                <link linkend="bitget">bitget</link>
            </member>
            <member>
                <link linkend="minus">minus</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>6.1.1</revnumber>
                <revdescription>
                    <itemizedlist>
                        <listitem>
                            Extension to 64 bit integers.
                        </listitem>
                        <listitem>
                            Extension to all signed integers.
                        </listitem>
                        <listitem>
                            Decimal positive integers > 2^52 up to 2^1024 = number_properties("huge")
                            can now be processed, with bitnum up to 1024 instead of 52.
                        </listitem>
                        <listitem>
                            bitnum is now optional as well for input decimal integers.
                            It can actually be an array.
                        </listitem>
                    </itemizedlist>
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
