<?xml version="1.0" encoding="UTF-8"?>
<!--
 *
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2016 - Samuel GOUGEON
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * 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="bitor" xml:lang="en">
    <refnamediv>
        <refname>bitor</refname>
        <refpurpose>bitwise logical OR between element-wise integers of 2 arrays</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            w = bitor(u, v)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Parameters</title>
        <variablelist>
            <varlistentry>
                <term>u, v, w</term>
                <listitem>
                    <para>
                        scalars, vectors, matrices or hypermatrices of null or
                        positive integers encoded as decimal or integer numbers
                        of any signed or unsigned <link linkend="inttype">inttype</link>.
                        <warning>Sparse-encoded matrices are not accepted.</warning>
                    </para>
                    <para>
                        If <literal>u</literal> and <literal>v</literal> have
                        the same type and inttype, this one is the working one.
                        Otherwise,
                        <itemizedlist>
                            <listitem>
                                if <literal>u</literal> or <literal>v</literal>
                                is decimal-encoded, the working inttype is 0
                                (real decimal), even if the other operand
                                is int64- or uint64-encoded.
                            </listitem>
                            <listitem>
                                if <literal>u</literal> and <literal>v</literal>
                                are both encoded integers, the working inttype
                                is the widest of both: int8 &lt; uint8 &lt;
                                int16 &lt; uint16 &lt; int32 &lt; uint32 &lt;
                                int64 &lt; uint64.
                            </listitem>
                        </itemizedlist>
                    </para>
                    <para>
                        The result <literal>w</literal> gets the type of the
                        working encoding.
                    </para>
                    <para>
                        <literal>u</literal> and <literal>v</literal> are
                        processed element-wise:
                        <itemizedlist>
                            <listitem>
                                If <literal>u</literal> is a single value (scalar)
                                and <literal>v</literal> is a vector, matrix or
                                hypermatrix, <literal>u</literal> is priorly
                                expanded as <literal>u*ones(v)</literal> in order
                                to operate <literal>u</literal> with every
                                <literal>v</literal> component.
                            </listitem>
                            <listitem>
                                Conversely, <literal>v</literal> is priorly
                                expanded as <literal>v*ones(u)</literal> if it
                                is a single value.
                            </listitem>
                            <listitem>
                                If neither <literal>u</literal> nor <literal>v</literal>
                                are scalars, they must have the same sizes.
                            </listitem>
                        </itemizedlist>
                    </para>
                    <para>
                        <literal>w</literal> gets the sizes of
                        <literal>u</literal> or/and <literal>v</literal>
                        arrays.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        For each pair of components <literal>u(i)</literal> and <literal>v(i)</literal>,
        <literal>bitor(u, v)</literal> computes and returns in <literal>w(i)</literal>
        the bitwise OR conjunction of <literal>u(i)</literal> and <literal>v(i)</literal>
        bits.
        <note>
            With encoded integers, <literal>bitor(u, v)</literal> is equivalent
            to <literal>u | v</literal>. However, <literal>u | v</literal>
            demands that <literal>u</literal> and <literal>v</literal> have
            the same inttype, while <literal>bitor(..)</literal> accepts
            mixed operands.
        </note>
        <note>
            For any decimal integer <literal>u</literal> greater than 2^52,
            only its bits from log2(u) down to log2(u)-52 are encoded and can
            be actually taken into account. Lower bits are not stored and are
            then ignored.
        </note>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
bitor(25, 33)
dec2bin([25 33 57]')  // binary representations
     ]]></programlisting>
        <screen><![CDATA[--> bitor(25, 33)
 ans  =
   57.

--> dec2bin([25 33 57]'))
 ans  =
!011001  !
!100001  !
!111001  !
]]></screen>
        <para></para>
        <programlisting role="example"><![CDATA[
// Between 2 simple rows with zeros and ones
u = [0 1 0 1];
v = [0 0 1 1];
bitor(u, v)  // [0 1 1 1] expected

// Encoded integers such as int8 are accepted:
u = int8([0 1 0 1]);
v = int8([0 0 1 1]);
bitor(u, v)

// Operands of mixed types are accepted.
// The type of the result is decimal if a decimal operand is involved,
// or the widest integer one otherwise:
u = [0 1 0 1];
v = [0 0 1 1];
z = bitor(u, int64(v));         type(z)       // 1 : decimal representation
z = bitor(uint8(u), int8(v));   typeof(z)     // uint8
z = bitor(uint8(u), int32(v));  typeof(z)     // int32

// Usage with 2 matrices
u = [  1  2  4  8
      25 33 25 33 ];
v = [  2  4  8 16
      33 25 57 57 ];
bitor(u, v)      //  [ 3 6 12 24 ;  57 57 57 57 ]   expected

// Usage with a distributed scalar:
bitor([1 2 4 8 9 10 12], 8)  // == bitor([1 2 4 8 9 10 12], [8 8 8 8 8 8 8])
bitor(4, [1 2 4 8 9 10 12])  // == bitor([4 4 4 4 4 4 4], [1 2 4 8 9 10 12])
    ]]></programlisting>
        <para></para>
        <programlisting role="example"><![CDATA[
// With encoded integers, bitor(u,v) and u|v are equivalent:
u = int8([2 3 10]);
v = int8(6);
[bitor(u, v) ; u | v]
// ... but "|" demands operands with the same type:
u | 6    // mismatching int8- | decimal- encodings
   ]]></programlisting>
        <screen><![CDATA[--> u = int8([2 3 10]);
--> v = int8(6);
--> [bitor(u, v) ; u | v]
 ans  =
  6  7  14
  6  7  14
--> u | 6
Undefined operation for the given operands.
check or define function %i_g_s for overloading.
]]></screen>
        <para></para>
        <programlisting role="example"><![CDATA[
// Examples with big decimal integers:

u = sum(2 .^(600+[0 3 9 20 45]))        // ~ 1.46D+194
v = 2^630                               // ~ 4.46D+189
w = bitor(u, v)
w == (u+v)  // must be true, since u is built without the bit #630
bitor(u, 2^645) == u   // true, since u has already its bit #645 set to 1
bitor(u, 2^601) == u   // false
//
n = fix(log2(u))       // Index of the heaviest bit of u
bitor(u, 2^(n-52)) == u   // false: The lightest bit of u was at 0 => This changes it
bitor(u, 2^(n-53)) == u   // true: Addressing bits below the lightest doesn't change u
   ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="or_op">|</link>
            </member>
            <member>
                <link linkend="or">or</link>
            </member>
            <member>
                <link linkend="bitxor">bitxor</link>
            </member>
            <member>
                <link linkend="bitand">bitand</link>
            </member>
            <member>
                <link linkend="dec2bin">dec2bin</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>6.0</revnumber>
                <revdescription>
                    <itemizedlist>
                        <listitem>
                            Extension to positive signed encoded integers
                        </listitem>
                        <listitem>
                            Extension to new int64 and uint64 inttypes
                        </listitem>
                        <listitem>
                            Operands of mixed types or inttypes are now accepted.
                        </listitem>
                        <listitem>
                            bitor(scalar, array) and bitor(array, scalar) are now supported.
                        </listitem>
                        <listitem>
                            Extension to decimal numbers greater than
                            2^32 and even than 2^52,
                            up to 1.80D+308 (== number_properties("huge")), for
                            the 52 heaviest encoded bits of their unsigned mantissa.
                        </listitem>
                    </itemizedlist>
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
